Authentication
Apple
Apple provider setup and usage.
Get your OAuth credentials
To use Apple sign in, you need a client ID, Team ID, Key ID, and private key. You can get them from the Apple Developer Portal, then use them to generate the client secret JWT.
You will need an active Apple Developer account to access the developer portal and generate these credentials.
Before you create anything, check two things:
- The team. The App ID, the Services ID and the key must all belong to the team that owns your app's bundle ID. If your Apple account is a member of several teams, switch to that team in the portal's team menu first. A Services ID or key created on another team fails at sign-in.
- The key. A
.p8file doesn't say what kind of key it is. An App Store Connect API key, an APNs key and a Sign in with Apple key look the same on disk. Only the portal's Keys page tells them apart, so don't reuse a.p8you already have unless that page shows it has Sign in with Apple enabled.
Follow these steps to set up your App ID, Service ID, and generate the key needed for your client secret:
-
Navigate to Certificates, Identifiers & Profiles: In the Apple Developer Portal, go to the "Certificates, Identifiers & Profiles" section.
-
Create an App ID:
- Go to the
Identifierstab. - Click the
+icon next to Identifiers. - Select
App IDs, then clickContinue. - Select
Appas the type, then clickContinue. - Description: Enter a name for your app (e.g., "My Awesome App"). This name may be displayed to users when they sign in.
- Bundle ID: Set a bundle ID. The recommended format is a reverse domain name (e.g.,
com.yourcompany.yourapp). Using a suffix like.ai(for app identifier) can help with organization but is not required (e.g.,com.yourcompany.yourapp.ai). - Scroll down to Capabilities. Select the checkbox for
Sign In with Apple. - Click
Continue, thenRegister.
- Go to the
-
Create a Service ID:
- Go back to the
Identifierstab. - Click the
+icon. - Select
Service IDs, then clickContinue. - Description: Enter a description for this service (e.g., your app name again).
- Identifier: Set a unique identifier for the service. Use a reverse domain format, distinct from your App ID (e.g.,
com.yourcompany.yourapp.si, where.siindicates service identifier - this is for your organization and not required). This Service ID will be yourclientId. - Click
Continue, thenRegister.
- Go back to the
-
Configure the Service ID:
- Find the Service ID you just created in the
Identifierslist and click on it. - Check the
Sign In with Applecapability, then clickConfigure. - Under Primary App ID, select the App ID you created earlier (e.g.,
com.yourcompany.yourapp.ai). Sign In with Apple must be turned on for the Service ID with a primary App ID before the portal lets you save domains and return URLs. - Under Domains and Subdomains, list all the root domains you will use for Sign In with Apple (e.g.,
example.com,anotherdomain.com). - Under Return URLs, enter the callback URL.
https://yourdomain.com/api/auth/callback/apple. Add all necessary return URLs. - Add every host you will test on, not only production: a staging domain, or the domain of an HTTPS tunnel you use during development. Apple refuses
localhostand plain HTTP. - Click
Next, thenDone. - Click
Continue, thenSave.
- Find the Service ID you just created in the
-
Create a Client Secret Key:
- Go to the
Keystab. - Click the
+icon to create a new key. - Key Name: Enter a name for the key (e.g., "Sign In with Apple Key").
- Scroll down and select the checkbox for
Sign In with Apple. - Click the
Configurebutton next toSign In with Apple. - Select the Primary App ID you created earlier.
- Click
Save, thenContinue, thenRegister. - Download the Key: Immediately download the
.p8key file. This file is only available for download once. Note the Key ID (available on the Keys page after creation) and your Team ID (available in your Apple Developer Account settings). - Store the
.p8file outside your repository, readable only by you (for examplechmod 600 AuthKey_<KEY_ID>.p8), and keep a copy in your secret manager. Never commit it.
- Go to the
-
Generate the Client Secret (JWT): Apple requires a JSON Web Token (JWT) to be generated dynamically using the downloaded
.p8key, the Key ID, and your Team ID. This JWT serves as yourclientSecret.Give Entro Auth the key, the Key ID and the Team ID as
signingKey, as in the example below, and it signs the client secret itself. You can also generate it manually by following Apple's documentation, then pass the generated JWT asclientSecret.Note: Apple rejects client secret JWTs that expire more than 15,777,000 seconds (six months) in the future. With
signingKeya new secret is signed before the last one ends; aclientSecretyou generated yourself has to be replaced before it expires.
Apple's OAuth implementation requires a JWT (JSON Web Token) as the clientSecret instead of a shared secret string.
According to Apple's documentation, these JWTs:
- Can't expire more than 15,777,000 seconds (six months) in the future
- Must be cryptographically signed with your private key
With signingKey (below), Entro Auth signs this JWT for you, so no extra package is needed. Install a JWT library such as jose only if you sign the secret yourself and pass it as clientSecret.
Configure the provider
To configure the provider, you need to add it to the socialProviders option of the auth instance.
You also need to add https://appleid.apple.com to the trustedOrigins array in your auth instance configuration to allow communication with Apple's authentication servers.
import { entroAuth } from "@entro314labs/entro-auth"
export const auth = entroAuth({
socialProviders: {
apple: {
clientId: process.env.APPLE_CLIENT_ID as string,
// Entro Auth signs the client secret from the key, and signs
// a new one before the last one ends.
signingKey: {
teamId: process.env.APPLE_TEAM_ID as string,
keyId: process.env.APPLE_KEY_ID as string,
privateKey: process.env.APPLE_PRIVATE_KEY as string, // the contents of the .p8 file
},
// Optional: makes the bundle ID the only accepted ID-token audience.
// To accept both, see "Native apps and the ID token audience" below.
appBundleIdentifier: process.env.APPLE_APP_BUNDLE_IDENTIFIER as string,
},
},
// Add appleid.apple.com to trustedOrigins for Sign In with Apple flows
trustedOrigins: ["https://appleid.apple.com"],
})Set the environment variables in your host's environment or secret manager: APPLE_CLIENT_ID (the Service ID), APPLE_TEAM_ID, APPLE_KEY_ID and APPLE_PRIVATE_KEY. APPLE_PRIVATE_KEY holds the contents of the .p8 file. When the key is given as signingKey.privateKey, its line breaks may be written as escaped \n sequences, as many hosts store multi-line values; Entro Auth turns them back into line breaks before signing. Load the value from the file rather than pasting it, and keep the .p8 itself outside the repository.
Native apps and the ID token audience
A native iOS app that uses Apple's sign-in sheet receives an ID token whose aud is the app's bundle ID, not the Service ID. When that token is passed to signIn.social (or linkSocial) as idToken, Entro Auth checks its aud against one source, in this order:
audience, when set;- otherwise
appBundleIdentifier, and then only the bundle ID; - otherwise every entry of
clientId.
A token whose aud isn't accepted fails with JWTClaimValidationFailed: unexpected "aud" claim value.
This check applies to ID-token sign-in only. The redirect flow is not affected: its callback reads the ID token returned by Apple's code exchange without this audience check. So setting only appBundleIdentifier keeps the web redirect working, but stops ID tokens issued for the Service ID from verifying.
To accept both, list the Service ID and the bundle ID together, with the Service ID first, because the authorization URL and the signed client secret use the first clientId entry:
apple: {
clientId: [
process.env.APPLE_CLIENT_ID as string, // Service ID, e.g. com.example.web
process.env.APPLE_APP_BUNDLE_IDENTIFIER as string, // bundle ID, e.g. com.example.app
],
signingKey: { /* ... */ },
},Or keep clientId as the Service ID and set audience: [serviceId, bundleId]. When the audience must depend on the request (for example reading an x-platform header), use a custom verifyIdToken callback — it receives the request context as its third argument.
Apple provides the user’s name only on first authorization. Email is included in subsequent ID tokens but may be empty for Work & School accounts.
Localhost and Non-TLS Restrictions
Apple Sign In does not support localhost or non-HTTPS URLs. During development:
- You cannot use
http://localhostas a return URL - You must use a domain with valid HTTPS/TLS certificate
This limitation is enforced by Apple's security requirements and cannot be bypassed.
Usage
Sign In with Apple
To sign in with Apple, you can use the signIn.social function provided by the client. The signIn function takes an object with the following properties:
provider: The provider to use. It should be set toapple.
import { createAuthClient } from "@entro314labs/entro-auth/client"
const authClient = createAuthClient()
const signIn = async () => {
const data = await authClient.signIn.social({
provider: "apple"
})
}Sign In with Apple With ID Token
To sign in with Apple using the ID Token, you can use the signIn.social function to pass the ID Token.
This is useful when you have the ID Token from Apple on the client-side and want to use it to sign in on the server.
If ID token is provided no redirection will happen, and the user will be signed in directly.
await authClient.signIn.social({
provider: "apple",
idToken: {
token: // Apple ID Token,
nonce: // Nonce (optional)
accessToken: // Access Token (optional)
}
})Verify your setup
Run these checks in order. Each one proves only its own part, so don't treat the setup as working until the last one passes.
- The provider is configured.
POST /api/auth/sign-in/socialwith the body{"provider": "apple", "disableRedirect": true}returns aurl. A 404PROVIDER_NOT_FOUNDmeans the running server didn't load the provider; aCLIENT_ID_AND_SECRET_REQUIREDerror in the server log means neitherclientSecretnorsigningKeyis set. If your UI decides at build time which buttons to show, redeploy after changing the provider's environment variables. - The authorization URL. The
urlstarts withhttps://appleid.apple.com/auth/authorize, itsclient_idis your Service ID, and itsredirect_uriis<baseURL>/api/auth/callback/apple. This proves what the server sends, not that Apple accepts it. - Apple's page. Open the
urlin a browser. Apple's sign-in page appears, notinvalid_client. This proves the Service ID and its return URL are registered. It doesn't prove the key: the client secret is used only in the code exchange after the user signs in. - A real sign-in. Sign in with an Apple account and land on your
callbackURLwith a session. Only this proves the private key, Team ID and Key ID. It needs a deployed HTTPS domain, or a tunnel domain registered on the Service ID. - Native apps. An ID-token sign-in from a device build returns a session. This proves the bundle ID is an accepted audience.
Troubleshooting
| Symptom | Cause |
|---|---|
invalid_client on Apple's page | clientId isn't the Service ID, or the redirect_uri isn't registered as a return URL for it. |
| The portal won't save domains or return URLs | Sign In with Apple isn't turned on for the Service ID with a primary App ID. |
| Return URL refused | localhost or plain HTTP. Use an HTTPS domain or a tunnel domain. |
invalid_code after Apple's page | Likely a wrong key, Team ID or Key ID, a key from another team, or a .p8 that isn't a Sign in with Apple key. This is inferred from the code path, not confirmed by testing. |
JWTClaimValidationFailed: unexpected "aud" claim value on ID-token sign-in | The token's aud (the bundle ID for the native sheet) isn't an accepted audience. See Native apps and the ID token audience. |
| An Apple sign-in doesn't reach the user's existing account | A user who picks Hide My Email shares a private relay address, which won't match an account created with their real email. Expect a new user, or signup_disabled when disableImplicitSignUp is set. Not confirmed by testing; check before you promise that Apple sign-in reaches existing accounts. Users can link Apple from a signed-in session instead. |
Sign-in button
Follow Apple's Human Interface Guidelines for Sign in with Apple, adapted to your design system:
- Use the official logo assets from Apple's design resources. Don't draw, crop or recolor the logo.
- Use an approved title only: "Sign in with Apple", "Sign up with Apple" or "Continue with Apple".
- Keep the logo and title both black or both white, on a black or white button: white on dark backgrounds; black, or white with an outline, on light ones.
- Match your other buttons where the guidelines allow it, such as the font, corner radius and the logo's inset.
- Don't make the Apple button smaller or less prominent than other sign-in buttons.
Apple's guidelines change; re-read them before shipping a new button.
App Store guideline 4.8. An iOS app whose sign-in offers a third-party login such as Google must also offer an equivalent option, such as Sign in with Apple. See the App Review Guidelines for the current wording and exceptions. Whether App Review accepts a browser-based Apple flow inside a native app, instead of the native sheet, is not confirmed; check before relying on it.
Generate Apple Client Secret (JWT)
If you previously used the in-page generator from this section, or generated the JWT yourself with jose in your configuration, use signingKey as in the example above instead. Entro Auth signs the client secret from your Client ID, Team ID, Key ID and private key when one is needed, keeps it for a day, and signs another before it ends, so there is no token to copy into your setup and none to renew.
Alternatively, you can still generate the client secret JWT manually by following Apple's documentation, then pass the generated JWT as clientSecret. If you choose this approach, remember to replace it before it expires.