$npx -y skills add dpearson2699/swift-ios-skills --skill authenticationImplement iOS authentication flows with AuthenticationServices and LocalAuthentication. Use when building Sign in with Apple, passkey/WebAuthn registration or sign-in with ASAuthorizationPlatformPublicKeyCredentialProvider, ASAuthorizationController credential state and revocatio
| 1 | # Authentication |
| 2 | |
| 3 | Implement authentication flows on iOS using the AuthenticationServices |
| 4 | framework, including Sign in with Apple, passkeys, OAuth/third-party web |
| 5 | auth, Password AutoFill, and biometric re-authentication. |
| 6 | |
| 7 | ## Contents |
| 8 | |
| 9 | - [Sign in with Apple](#sign-in-with-apple) |
| 10 | - [Credential Handling](#credential-handling) |
| 11 | - [Credential State Checking](#credential-state-checking) |
| 12 | - [Token Validation](#token-validation) |
| 13 | - [Existing Account Setup Flows](#existing-account-setup-flows) |
| 14 | - [Passkeys](#passkeys) |
| 15 | - [ASWebAuthenticationSession (OAuth)](#aswebauthenticationsession-oauth) |
| 16 | - [Password AutoFill Credentials](#password-autofill-credentials) |
| 17 | - [Biometric Authentication](#biometric-authentication) |
| 18 | - [Security Boundaries](#security-boundaries) |
| 19 | - [SwiftUI SignInWithAppleButton](#swiftui-signinwithapplebutton) |
| 20 | - [Common Mistakes](#common-mistakes) |
| 21 | - [Review Checklist](#review-checklist) |
| 22 | - [References](#references) |
| 23 | |
| 24 | ## Sign in with Apple |
| 25 | |
| 26 | Add the "Sign in with Apple" capability in Xcode before using these APIs. |
| 27 | |
| 28 | ### UIKit: ASAuthorizationController Setup |
| 29 | |
| 30 | ```swift |
| 31 | import AuthenticationServices |
| 32 | |
| 33 | final class LoginViewController: UIViewController { |
| 34 | func startSignInWithApple() { |
| 35 | let provider = ASAuthorizationAppleIDProvider() |
| 36 | let request = provider.createRequest() |
| 37 | request.requestedScopes = [.fullName, .email] |
| 38 | |
| 39 | let controller = ASAuthorizationController(authorizationRequests: [request]) |
| 40 | controller.delegate = self |
| 41 | controller.presentationContextProvider = self |
| 42 | controller.performRequests() |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | extension LoginViewController: ASAuthorizationControllerPresentationContextProviding { |
| 47 | func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor { |
| 48 | view.window! |
| 49 | } |
| 50 | } |
| 51 | ``` |
| 52 | |
| 53 | ### Delegate: Handling Success and Failure |
| 54 | |
| 55 | ```swift |
| 56 | extension LoginViewController: ASAuthorizationControllerDelegate { |
| 57 | func authorizationController( |
| 58 | controller: ASAuthorizationController, |
| 59 | didCompleteWithAuthorization authorization: ASAuthorization |
| 60 | ) { |
| 61 | guard let credential = authorization.credential |
| 62 | as? ASAuthorizationAppleIDCredential else { return } |
| 63 | |
| 64 | let userID = credential.user // Stable, unique, per-team identifier |
| 65 | let email = credential.email // nil after first authorization |
| 66 | let fullName = credential.fullName // nil after first authorization |
| 67 | let identityToken = credential.identityToken // JWT for server validation |
| 68 | let authCode = credential.authorizationCode // Short-lived code for server exchange |
| 69 | |
| 70 | // Save userID to Keychain for credential state checks |
| 71 | // See references/keychain-biometric.md for Keychain patterns |
| 72 | saveUserID(userID) |
| 73 | |
| 74 | // Send identityToken and authCode to your server |
| 75 | authenticateWithServer(identityToken: identityToken, authCode: authCode) |
| 76 | } |
| 77 | |
| 78 | func authorizationController( |
| 79 | controller: ASAuthorizationController, |
| 80 | didCompleteWithError error: any Error |
| 81 | ) { |
| 82 | switch (error as? ASAuthorizationError)?.code { |
| 83 | case .canceled, .notInteractive: |
| 84 | break |
| 85 | case .failed: |
| 86 | showError("Authorization failed") |
| 87 | default: |
| 88 | showError("Authorization failed: \(error.localizedDescription)") |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | ``` |
| 93 | |
| 94 | ## Credential Handling |
| 95 | |
| 96 | | Credential data | Required handling | |
| 97 | |---|---| |
| 98 | | `user` | Persist this stable, per-team identifier for credential-state checks. | |
| 99 | | `email`, `fullName` | These optional values arrive only on first authorization; cache them immediately. | |
| 100 | | `identityToken`, `authorizationCode` | Send them to the server for validation or exchange; never trust them as client-side proof. | |
| 101 | |
| 102 | Treat `realUserStatus` only as a fraud-prevention signal, not authentication |
| 103 | proof. |
| 104 | |
| 105 | ## Credential State Checking |
| 106 | |
| 107 | Check credential state on every app launch. The user may revoke access at |
| 108 | any time via Settings > Apple Account > Sign-In & Security. |
| 109 | |
| 110 | ```swift |
| 111 | func checkCredentialState() { |
| 112 | let provider = ASAuthorizationAppleIDProvider() |
| 113 | guard let userID = loadSavedUserID() else { |
| 114 | showLoginScreen() |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | provider.getCredentialState(forUserID: userID) { state, _ in |
| 119 | DispatchQueue.main.async { |
| 120 | switch state { |
| 121 | case .authorized: |
| 122 | proceedToMainApp() |
| 123 | case .revoked: |
| 124 | // User revoked -- sign out and clear local data |
| 125 | signOut() |
| 126 | showLog |