Prompt
How do I implement Sign in with Apple on web and mobile?
Latest observation
Implementing Sign in with Apple differs a bit between web and mobile, but the overall flow is the same:
- Register your app/service in Apple Developer
- Generate/configure the correct identifiers and keys
- Start the Apple authorization request
- Handle the redirect / callback
- Verify the returned identity token on your backend
- Create or link your user account in your system
Below is a practical breakdown for both.
1) What you need in Apple Developer
Create/Configure:
- App ID / Bundle ID for iOS/macOS app
- Service ID for web sign-in
- Private key (
.p8) for generating client secrets on your backend - Team ID
- Key ID
- Redirect URI(s) for the web flow
- Enable Sign in with Apple capability
Important note
For web and mobile, Apple returns an authorization code and an identity token (JWT). Your backend should verify the JWT and exchange the code for tokens where appropriate.
2) Web implementation
Web Sign in with Apple uses the OAuth 2.0/OpenID Connect flow.
A. Configure in Apple Developer
In your Service ID:
- Enable Sign in with Apple
- Add your Return URLs (redirect URIs), e.g.
https://example.com/auth/apple/callback
B. Frontend: start authorization
You can use Apple’s JS library or construct the authorization URL manually.
Using Apple JS
Include:
<script src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
Initialize:
<script>
AppleID.auth.init({
clientId: 'com.example.web',
scope: 'name email',
redirectURI: 'https://example.com/auth/apple/callback',
state: 'random_csrf_token',
usePopup: true
});
</script>
Then trigger:
<button id="apple-signin">Sign in with Apple</button>
<script>
document.getElementById('apple-signin').addEventListener('click', () => {
AppleID.auth.signIn();
});
</script>
C. Callback handling
Apple redirects back with:
codeid_tokenstate
Your backend should:
- Validate
state - Verify
id_tokensignature and claims - Exchange
codefor tokens if needed
D. Backend token exchange
You send a POST to Apple’s token endpoint:
https://appleid.apple.com/auth/token
Required fields:
client_idclient_secret(JWT you generate with your Apple private key)codegrant_type=authorization_coderedirect_uri
Generate client_secret
This is a JWT signed with your Apple .p8 key containing:
iss= Team IDiatexp(up to 6 months from issuance)aud=https://appleid.apple.comsub= Service ID / client_id
You generally generate this on your backend.
E. Verify identity token
Apple’s id_token is a JWT. Verify:
- signature using Apple public keys
iss == https://appleid.apple.comaud == your client_idexpnot expirednonceif you used one
F. User data
Apple may provide:
emailemail_verifiedis_private_emailsub(stable unique user identifier)
Note:
- name is only returned the first time the user authorizes your app.
- You must store it immediately if you need it.
3) iOS / mobile implementation
For iOS, use AuthenticationServices.
A. Add capability
In Xcode:
- Add Sign In with Apple capability to your app target
B. Native Swift example
import AuthenticationServices
class ViewController: UIViewController, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
func signInWithApple() {
let provider = ASAuthorizationAppleIDProvider()
let request = provider.createRequest()
request.requestedScopes = [.fullName, .email]
// Optional nonce for backend verification
let nonce = UUID().uuidString
request.nonce = sha256(nonce)
let controller = ASAuthorizationController(authorizationRequests: [request])
controller.delegate = self
controller.presentationContextProvider = self
controller.performRequests()
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let credential = authorization.credential as? ASAuthorizationAppleIDCredential {
let userId = credential.user
let identityToken = credential.identityToken
let authorizationCode = credential.authorizationCode
let email = credential.email
let fullName = credential.fullName
// Send identityToken and authorizationCode to your backend
}
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
print("Apple Sign-In failed: \(error)")
}
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}
}
C. Backend verification
Same idea as web:
- Verify the
identityTokenJWT - Optionally exchange
authorizationCodeat Apple’s token endpoint - Use
subas the stable Apple user ID
D. Reuse login state
You can check whether a credential is still valid:
let provider = ASAuthorizationAppleIDProvider()
provider.getCredentialState(forUserID: userId) { state, error in
switch state {
case .authorized:
print("Still signed in")
case .revoked, .notFound:
print("Needs to sign in again")
default:
break
}
}
4) Android / cross-platform mobile
Apple doesn’t offer a native Android SDK, so mobile web-based OAuth flow is usually used.
Common approaches:
- Open Apple’s sign-in page in a system browser / custom tab
- Use a framework/plugin for OAuth/OpenID Connect
- Handle redirect back to your app using a deep link
You’ll still:
- use a Service ID
- set a redirect URI
- verify the token on your backend
5) Backend verification essentials
Whether web or mobile, your backend should:
- Treat Apple as an identity provider
- Never trust the frontend alone
- Verify
id_token - Map Apple’s
subto your internal user record - Support account linking if the user later signs in via email/password or another provider
Recommended claims to validate in JWT
iss = https://appleid.apple.comaud = client_idexp > nownoncematches if usedsubexists
6) Common pitfalls
- Forgetting to save the user’s name the first time
- Using the wrong client_id
- Web: Service ID
- Native iOS: Bundle ID / App ID
- Not validating the JWT on the backend
- Using an expired
client_secret - Missing or incorrect redirect URI
- Expecting Apple to always return email/name
- Not handling Hide My Email
7) Recommended architecture
A common setup:
Web:
- Frontend triggers Apple login
- Apple redirects to backend callback
- Backend validates and creates session/JWT for your app
iOS:
- App gets authorization
- App sends
identityToken+authorizationCodeto backend - Backend validates and creates session/JWT
8) If you want a production-ready flow
I can provide any of these next:
- Full web example with Node.js/Express or Python/Flask
- Full iOS Swift implementation
- Backend JWT verification code
- React / Next.js implementation
- Android/Cross-platform OAuth setup
If you tell me your stack, I can give you a copy-paste implementation.