Skip to content

Instantly share code, notes, and snippets.

@Kaival-Patel
Last active January 20, 2023 10:04
Show Gist options
  • Select an option

  • Save Kaival-Patel/5ff4f9921d88d49bee544eb3a3b10917 to your computer and use it in GitHub Desktop.

Select an option

Save Kaival-Patel/5ff4f9921d88d49bee544eb3a3b10917 to your computer and use it in GitHub Desktop.
Authorization Controller Implementation for Error and success handling callbacks
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
let logger = Logger()
switch authorization.credential {
case let credentialRegistration as ASAuthorizationPlatformPublicKeyCredentialRegistration:
logger.log("A new passkey was registered: \(credentialRegistration)")
// Verify the attestationObject and clientDataJSON with your service.
// The attestationObject contains the user's new public key to store and use for subsequent sign-ins.
// let attestationObject = credentialRegistration.rawAttestationObject
// let clientDataJSON = credentialRegistration.rawClientDataJSON
// After the server verifies the registration and creates the user account, sign in the user with the new account.
didFinishSignIn()
case let credentialAssertion as ASAuthorizationPlatformPublicKeyCredentialAssertion:
logger.log("A passkey was used to sign in: \(credentialAssertion)")
// Verify the below signature and clientDataJSON with your service for the given userID.
// let signature = credentialAssertion.signature
// let clientDataJSON = credentialAssertion.rawClientDataJSON
// let userID = credentialAssertion.userID
// After the server verifies the assertion, sign in the user.
didFinishSignIn()
case let passwordCredential as ASPasswordCredential:
logger.log("A password was provided: \(passwordCredential)")
// Verify the userName and password with your service.
// let userName = passwordCredential.user
// let password = passwordCredential.password
// After the server verifies the userName and password, sign in the user.
didFinishSignIn()
default:
fatalError("Received unknown authorization type.")
}
isPerformingModalReqest = false
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
let logger = Logger()
guard let authorizationError = error as? ASAuthorizationError else {
isPerformingModalReqest = false
logger.error("Unexpected authorization error: \(error.localizedDescription)")
return
}
if authorizationError.code == .canceled {
// Either the system doesn't find any credentials and the request ends silently, or the user cancels the request.
// This is a good time to show a traditional login form, or ask the user to create an account.
logger.log("Request canceled.")
if isPerformingModalReqest {
didCancelModalSheet()
}
} else {
// Another ASAuthorization error.
// Note: The userInfo dictionary contains useful information.
logger.error("Error: \((error as NSError).userInfo)")
}
isPerformingModalReqest = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment