On this page

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 .p8 file 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 .p8 you 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:

  1. Navigate to Certificates, Identifiers & Profiles: In the Apple Developer Portal, go to the "Certificates, Identifiers & Profiles" section.

  2. Create an App ID:

    • Go to the Identifiers tab.
    • Click the + icon next to Identifiers.
    • Select App IDs, then click Continue.
    • Select App as the type, then click Continue.
    • 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, then Register.
  3. Create a Service ID:

    • Go back to the Identifiers tab.
    • Click the + icon.
    • Select Service IDs, then click Continue.
    • 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 .si indicates service identifier - this is for your organization and not required). This Service ID will be your clientId.
    • Click Continue, then Register.
  4. Configure the Service ID:

    • Find the Service ID you just created in the Identifiers list and click on it.
    • Check the Sign In with Apple capability, then click Configure.
    • 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 localhost and plain HTTP.
    • Click Next, then Done.
    • Click Continue, then Save.
  5. Create a Client Secret Key:

    • Go to the Keys tab.
    • 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 Configure button next to Sign In with Apple.
    • Select the Primary App ID you created earlier.
    • Click Save, then Continue, then Register.
    • Download the Key: Immediately download the .p8 key 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 .p8 file outside your repository, readable only by you (for example chmod 600 AuthKey_<KEY_ID>.p8), and keep a copy in your secret manager. Never commit it.
  6. Generate the Client Secret (JWT): Apple requires a JSON Web Token (JWT) to be generated dynamically using the downloaded .p8 key, the Key ID, and your Team ID. This JWT serves as your clientSecret.

    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 as clientSecret.

    Note: Apple rejects client secret JWTs that expire more than 15,777,000 seconds (six months) in the future. With signingKey a new secret is signed before the last one ends; a clientSecret you 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.

auth.ts
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:

  1. audience, when set;
  2. otherwise appBundleIdentifier, and then only the bundle ID;
  3. 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:

auth.ts
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.

Warning

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.

Warning

Localhost and Non-TLS Restrictions

Apple Sign In does not support localhost or non-HTTPS URLs. During development:

  • You cannot use http://localhost as 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 to apple.
auth-client.ts
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.

Note

If ID token is provided no redirection will happen, and the user will be signed in directly.

auth-client.ts
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.

  1. The provider is configured. POST /api/auth/sign-in/social with the body {"provider": "apple", "disableRedirect": true} returns a url. A 404 PROVIDER_NOT_FOUND means the running server didn't load the provider; a CLIENT_ID_AND_SECRET_REQUIRED error in the server log means neither clientSecret nor signingKey is set. If your UI decides at build time which buttons to show, redeploy after changing the provider's environment variables.
  2. The authorization URL. The url starts with https://appleid.apple.com/auth/authorize, its client_id is your Service ID, and its redirect_uri is <baseURL>/api/auth/callback/apple. This proves what the server sends, not that Apple accepts it.
  3. Apple's page. Open the url in a browser. Apple's sign-in page appears, not invalid_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.
  4. A real sign-in. Sign in with an Apple account and land on your callbackURL with 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.
  5. Native apps. An ID-token sign-in from a device build returns a session. This proves the bundle ID is an accepted audience.

Troubleshooting

SymptomCause
invalid_client on Apple's pageclientId 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 URLsSign In with Apple isn't turned on for the Service ID with a primary App ID.
Return URL refusedlocalhost or plain HTTP. Use an HTTPS domain or a tunnel domain.
invalid_code after Apple's pageLikely 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-inThe 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 accountA 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.

Note

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.