Skip to content

Commit

Permalink
Add apple login to example app
Browse files Browse the repository at this point in the history
  • Loading branch information
nkshah2 committed Nov 21, 2023
1 parent 67deadf commit 57c5ec4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)")
}
}
10 changes: 10 additions & 0 deletions examples/with-thirdparty/with-thirdparty/LoginScreen/LoginView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
<action selector="onGithubClicked" destination="-1" eventType="touchUpInside" id="Kqd-7T-jGw"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="H2q-wu-yPk">
<rect key="frame" x="108.00000000000001" y="482" width="177.33333333333337" height="35"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="filled" title="Continue with Apple"/>
<connections>
<action selector="onAppleClicked" destination="-1" eventType="touchUpInside" id="6nF-x7-jcV"/>
</connections>
</button>
</subviews>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
Expand All @@ -43,6 +51,8 @@
<constraint firstItem="JQl-ue-ADz" firstAttribute="top" secondItem="kNW-UC-IRU" secondAttribute="bottom" constant="12" id="Mbw-RY-D2T"/>
<constraint firstItem="kNW-UC-IRU" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" constant="-20" id="ayv-ki-YVU"/>
<constraint firstItem="kNW-UC-IRU" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="uAk-MN-rb7"/>
<constraint firstItem="H2q-wu-yPk" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="una-Ps-vEM"/>
<constraint firstItem="H2q-wu-yPk" firstAttribute="top" secondItem="JQl-ue-ADz" secondAttribute="bottom" constant="12" id="xpV-aq-NuZ"/>
</constraints>
<point key="canvasLocation" x="41" y="21"/>
</view>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.applesignin</key>
<array>
<string>Default</string>
</array>
</dict>
</plist>

0 comments on commit 57c5ec4

Please sign in to comment.