From 9ec7023ac30de7a2daf542a857a21aef92e6ff65 Mon Sep 17 00:00:00 2001 From: Nemi Shah Date: Tue, 21 Nov 2023 15:00:13 +0530 Subject: [PATCH] Add apple login to example app (#59) --- .../LoginScreenViewController.swift | 68 ++++++++++++++++++- .../with-thirdparty/LoginScreen/LoginView.xib | 10 +++ .../with-thirdparty.entitlements | 10 +++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 examples/with-thirdparty/with-thirdparty/with-thirdparty.entitlements diff --git a/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginScreenViewController.swift b/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginScreenViewController.swift index 8b5acad..21f65f0 100644 --- a/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginScreenViewController.swift +++ b/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginScreenViewController.swift @@ -8,8 +8,10 @@ import UIKit import GoogleSignIn import AppAuth +import AuthenticationServices -class LoginScreenViewController: UIViewController { +class LoginScreenViewController: UIViewController, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding { + override func viewDidLoad() { super.viewDidLoad() } @@ -120,4 +122,68 @@ class LoginScreenViewController: UIViewController { } }) } + + func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor { + return self.view.window! + + } + + @IBAction func onAppleClicked() { + let appleIDProvider = ASAuthorizationAppleIDProvider() + let request = appleIDProvider.createRequest() + request.requestedScopes = [.email] + + let authorizationController = ASAuthorizationController(authorizationRequests: [request]) + authorizationController.delegate = self + authorizationController.presentationContextProvider = self + authorizationController.performRequests() + } + + func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { + guard case let appleIDCredential as ASAuthorizationAppleIDCredential = authorization.credential, let authCodeResponse: Data = appleIDCredential.authorizationCode else { + print("Apple returned an unexpected response") + return + } + + let authCode = String(decoding: authCodeResponse, as: UTF8.self) + + let url = URL(string: Constants.apiDomain + "/auth/signinup") + var request = URLRequest(url: url!) + request.httpMethod = "POST" + + let data = try! JSONSerialization.data(withJSONObject: [ + "thirdPartyId": "apple", + "redirectURIInfo": [ + // For native flows we do not have a redirect uri + "redirectURIOnProviderDashboard": "", + "redirectURIQueryParams": [ + "code": authCode + ], + ], + ]) + request.httpBody = data + request.setValue("Application/json", forHTTPHeaderField: "Content-Type") + + URLSession.shared.dataTask(with: request) { + data, response, error in + + if error != nil { + print("Apple login failed: \(error!.localizedDescription)") + } + + if let _response: URLResponse = response, let httpResponse: HTTPURLResponse = _response as? HTTPURLResponse { + if httpResponse.statusCode == 200 { + DispatchQueue.main.async { [weak self] in + self?.navigationController?.pushViewController(HomeScreenViewController(nibName: "HomeView", bundle: nil), animated: true) + } + } else { + print("SuperTokens API failed with code: \(httpResponse.statusCode)") + } + } + }.resume() + } + + func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { + print("Apple login failed: \(error.localizedDescription)") + } } diff --git a/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginView.xib b/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginView.xib index 2f4ef4d..5501752 100644 --- a/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginView.xib +++ b/examples/with-thirdparty/with-thirdparty/LoginScreen/LoginView.xib @@ -35,6 +35,14 @@ + @@ -43,6 +51,8 @@ + + diff --git a/examples/with-thirdparty/with-thirdparty/with-thirdparty.entitlements b/examples/with-thirdparty/with-thirdparty/with-thirdparty.entitlements new file mode 100644 index 0000000..a812db5 --- /dev/null +++ b/examples/with-thirdparty/with-thirdparty/with-thirdparty.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.developer.applesignin + + Default + + +