Created
January 20, 2023 09:35
-
-
Save Kaival-Patel/1aba36cd80c342d9ef80e096cc003fc4 to your computer and use it in GitHub Desktop.
SignInMethod Implementation for Passkeys
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
| func signInWith(anchor: ASPresentationAnchor, preferImmediatelyAvailableCredentials: Bool) { | |
| self.authenticationAnchor = anchor | |
| let publicKeyCredentialProvider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: domain) | |
| // Fetch the challenge from the server. The challenge needs to be unique for each request. | |
| let challenge = Data() | |
| let assertionRequest = publicKeyCredentialProvider.createCredentialAssertionRequest(challenge: challenge) | |
| // Also allow the user to use a saved password, if they have one. | |
| let passwordCredentialProvider = ASAuthorizationPasswordProvider() | |
| let passwordRequest = passwordCredentialProvider.createRequest() | |
| // Pass in any mix of supported sign-in request types. | |
| let authController = ASAuthorizationController(authorizationRequests: [ assertionRequest, passwordRequest ] ) | |
| authController.delegate = self | |
| authController.presentationContextProvider = self | |
| if preferImmediatelyAvailableCredentials { | |
| // If credentials are available, presents a modal sign-in sheet. | |
| // If there are no locally saved credentials, no UI appears and | |
| // the system passes ASAuthorizationError.Code.canceled to call | |
| // `AccountManager.authorizationController(controller:didCompleteWithError:)`. | |
| authController.performRequests(options: .preferImmediatelyAvailableCredentials) | |
| } else { | |
| // If credentials are available, presents a modal sign-in sheet. | |
| // If there are no locally saved credentials, the system presents a QR code to allow signing in with a | |
| // passkey from a nearby device. | |
| authController.performRequests() | |
| } | |
| isPerformingModalReqest = true | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment