LynxAppleSignIn
Signs the person in with Sign in with Apple's own sheet on iOS, and gives you the identity token and authorization code your server checks with Apple. import { LynxAppleSignIn, AppleSignInError } from '@blotch/angular-lynx';
Turn it on
Add signInWithApple to the build target's options in angular.json, with your team's ios.teamId:
What it adds:
- iOS: the
com.apple.developer.applesigninentitlement, the native code behind the sheet, and iOS's own<apple-sign-in-button>. Xcode turns on Sign in with Apple for the App ID when it makes the provisioning profile. - Android: nothing. The sheet is iOS's alone.
Without it, the entitlement and the native code are left out. isAvailable() resolves false, and signIn() rejects with unsupported.
Signing the entitlement needs a team in the paid Apple Developer Program. A free Personal Team can't sign it, and the build says so. The Simulator app needs no team.
See Turn on device features for every setting.
Usage
Auth stands for the app's own service, which sends the result to your server.
Use isAvailable() to choose a sign-in. In the Android app, sign in with Apple's web flow through LynxWebAuthSession instead. The web preview and Lynx Explorer have neither.
Start it from <apple-sign-in-button>, which App Review checks against Apple's Sign in with Apple guidelines.
- In the iOS app it's iOS's own button, with the logo, the title in the phone's language, and Apple's colors and corners.
- Elsewhere the runtime draws the same button.
- Keep it at least as prominent as the other logins.
Check the result on your server
The app can't trust what it gets back by itself. Send identityToken and authorizationCode to your server, which:
-
Verifies
identityToken, a JWT, against Apple's public keys (https://appleid.apple.com/auth/keys). It checks that:- its
issishttps://appleid.apple.com - its
audis your app ID - its
nonceis the hash of yours, if you sent one
The token's
subis the person's ID. Key the account on it, never on auserthe app sends alongside, which anyone can change. - its
-
Exchanges
authorizationCodeathttps://appleid.apple.com/auth/tokenfor a refresh token. Keep it: revoking it is how the server deletes the person's Apple sign-in when they delete their account. Guideline 5.1.1(v) requires that.
Services such as Firebase Authentication and Supabase do step 1. Pass them the token, with the raw nonce if you sent one.
- Their user has its own ID, which they link to the token's
sub. Key the account on theirs. - They don't keep Apple's refresh token, so plan step 2 yourself. Firebase, for one, revokes with
revokeToken(withAuthorizationCode:), which needs a fresh code from a new sign-in. Check your service's account deletion guide.
Use a nonce
A nonce ties the identity token to one sign-in, so a token someone copies can't be replayed:
- Make a random string.
- Pass its SHA-256 as
nonce. - Send the string itself to the server. It checks that the token's
nonceclaim is the string's hash.
That's the form Firebase and Supabase take.
Check the sign-in at launch
The person can stop using their Apple ID with your app in Settings. Apple asks apps to check at launch, and sign the person out when it's revoked.
The check takes Apple's user from the sign-in. So keep it on the device, even when your account uses another ID, such as a Firebase UID:
When App Review asks for it
App Review usually asks for it beside other social logins. Under Apple's guideline 4.8, an app whose main login is Google, Facebook or another third-party service must also offer a login that:
- limits what it collects to a name and email
- lets the person hide their email
- doesn't track them for ads
Sign in with Apple meets that. The guideline has exceptions, such as an app that signs in only with its own accounts, or with a company's.
The native sheet is also the one people expect on iOS: Face ID, then done, with no web page.
Platforms
On a phone or Simulator signed in to no Apple Account, iOS offers Settings instead of the sheet. Closing that rejects with failed.
API
LynxAppleSignIn
isAvailable()
Resolves true where signIn() opens the native sheet: an iOS app built with signInWithApple. false everywhere else. It never rejects.
signIn()
Opens the sheet and resolves with the credential once the person signs in. Rejects with an AppleSignInError.
Apple shares the email and name once, on the person's first sign-in to your app. Later sign-ins return user and the tokens alone, so save them the first time. Key the account on this ID, not the email, which the person can hide behind a relay address, and change. Your server reads the ID from the verified token's sub, which equals user.
getCredentialState()
Asks Apple whether user, from an earlier sign-in, is still signed in. Rejects with an AppleSignInError: unsupported where signIn() would.
AppleSignInError
A failed error's message ends with Apple's error code. Code 1000 also comes from a phone signed in to no Apple Account, and from an app signed without the entitlement.
Testing
The testing library has a fake of the native module. Until a spec says how the person signs in, they close the sheet, and signIn() rejects with canceled:
See setAppleSignInResult() for the rest: failing a sign-in, taking the sheet away as on Android, and a user's credential state.
Related
<apple-sign-in-button>: the button that starts it- Sign in with a web page: Apple's web flow, for Android
- Turn on device features:
signInWithAppleand the other opt-in settings
