Last active
October 17, 2022 17:56
-
-
Save sergiocampama/4c8ffacbce3e21a247e00b66f70022b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<script type="module"> | |
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.9.1/firebase-app.js"; | |
import { OAuthProvider, getAuth, signInWithRedirect, getRedirectResult } from 'https://www.gstatic.com/firebasejs/9.9.1/firebase-auth.js' | |
import { getFunctions, httpsCallable } from 'https://www.gstatic.com/firebasejs/9.9.1/firebase-functions.js' | |
const params = new Proxy(new URLSearchParams(window.location.search), { | |
get: (searchParams, prop) => searchParams.get(prop), | |
}); | |
if (params.platform != "catalyst") { | |
window.location = "https://copilot.money"; | |
} else { | |
const firebaseConfig = { | |
apiKey: "AIzaSyAMgjkeOSkHj4J4rlswOkD16N3WQOoNPpk", | |
authDomain: "copilot.money", | |
databaseURL: "https://copilot-production-22904.firebaseio.com", | |
projectId: "copilot-production-22904", | |
storageBucket: "copilot-production-22904.appspot.com", | |
messagingSenderId: "445606440735", | |
appId: "1:445606440735:web:b0ac6d52da1d4c16c90a2a", | |
measurementId: "G-T4HCG2XFK0" | |
}; | |
const app = initializeApp(firebaseConfig); | |
const auth = getAuth(); | |
const provider = new OAuthProvider('apple.com'); | |
provider.addScope('email'); | |
getRedirectResult(auth) | |
.then((result) => { | |
if (result == null) { | |
signInWithRedirect(auth, provider); | |
} else { | |
const credential = OAuthProvider.credentialFromResult(result); | |
if (credential) { | |
const accessToken = credential.accessToken; | |
const idToken = credential.idToken; | |
} | |
const functions = getFunctions() | |
httpsCallable(functions, "createCustomToken", null)(null) | |
.then((result) => { | |
console.log(result.data); | |
window.location = "copilot-auth://authenticated?token=" + result.data; | |
}) | |
.catch((error) => { | |
console.log("functions error"); | |
console.log(error); | |
}); | |
} | |
}) | |
.catch((error) => { | |
console.log("got error"); | |
console.log(error); | |
const errorCode = error.code; | |
if (errorCode == "auth/user-cancelled") { | |
// Handle case where user canceled | |
} else { | |
// Handle generic errors | |
} | |
}); | |
} | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import WebKit | |
final class SignInWithAppleViewController: UIViewController { | |
private let webView: WKWebView = { | |
let configuration = WKWebViewConfiguration() | |
return WKWebView(frame: .zero, configuration: configuration) | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(webView) | |
webView.navigationDelegate = self | |
webView.uiDelegate = self | |
} | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
webView.frame = view.bounds | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
webView.load(URLRequest(url: URL(string: "https://copilot.money/oauth/apple?platform=catalyst")!)) | |
} | |
} | |
extension SignInWithAppleViewController: WKNavigationDelegate, WKUIDelegate { | |
func webView( | |
_ webView: WKWebView, | |
decidePolicyFor navigationAction: WKNavigationAction, | |
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void | |
) { | |
guard let url = navigationAction.request.url else { | |
decisionHandler(.cancel) | |
return | |
} | |
let urlString = url.absoluteString | |
guard urlString.hasPrefix("copilot-auth://") else { | |
decisionHandler(.allow) | |
return | |
} | |
defer { decisionHandler(.cancel) } | |
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false), | |
let authToken = components.queryItems?.first?.value | |
else { | |
return | |
} | |
Dep(AuthenticationController.self).signIn(withCustomToken: authToken) { _ in | |
self.dismiss(animated: true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment