Skip to content

Sign in with Apple

Your users sign in to your app with their Apple ID. As with Google, your app receives an ID token, from Apple this time, and exchanges it for an ObjectsGrid access token. ObjectsGrid creates the user's User Account on their first sign-in.

If your iOS app offers Sign in with Google, Apple's App Store Review Guidelines (4.8) ask it to offer an equivalent privacy-focused sign-in too: Sign in with Apple is one.

This guide takes you through it in four steps:

  1. Register your app with Apple: a Services ID for a website, the bundle id for an iOS app.
  2. Add the Sign in with Apple button to your app.
  3. Register the Services ID or bundle id with your Organization Account.
  4. Exchange the ID token for an access token.
sequenceDiagram
    participant User
    participant App as Your app
    participant Apple
    participant OG as ObjectsGrid
    User->>App: Clicks "Sign in with Apple"
    App->>Apple: Sign-in request with your Services ID and a nonce
    Apple-->>App: ID token
    App->>OG: POST /oauth2/token with the ID token and the nonce
    OG-->>App: Access token and refresh token
    App->>OG: API requests with Authorization: Bearer <access token>

1. Register your app with Apple

You need a membership of the Apple Developer Program. In Certificates, Identifiers & Profiles:

  1. App ID. Under Identifiers, click +, choose App IDs, then App. Give it a description and an explicit bundle id, such as com.example.shop, tick Sign in with Apple, and register it. An iOS app signs in with this bundle id.
  2. Services ID, for a website. Click + again and choose Services IDs. Give it a description, which users may see when they sign in, and an identifier different from the bundle id, such as com.example.shop.web. Register it, open it, tick Sign in with Apple and click Configure:

    • Primary App ID: the App ID of step 1.
    • Domains and Subdomains: your site's host name, such as shop.example.com, without https://.
    • Return URLs: the page that shows the button, such as https://shop.example.com. Apple refuses localhost.

    Click Next, Done, Continue and Save. Use one Services ID per environment, as you would one Google client per environment. 3. Email relay. Users may hide their email: Apple then gives your app an address such as …@privaterelay.appleid.com, which forwards to them. To send them email, register the domains and addresses you send from under Services, Sign in with Apple for Email Communication; Apple accepts only mail that passes SPF for them.

ObjectsGrid only verifies the ID tokens Apple issues, with Apple's public keys: you don't create a Sign in with Apple key for it.

2. Add the button to your app

On the web, Apple's Sign in with Apple JS draws the button. In popup mode it hands your page the ID token, so no server of yours receives Apple's form post. Generate a fresh nonce for every sign-in and give it to Apple; you send the same value to ObjectsGrid.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sign in</title>
  <script src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
</head>
<body>
  <div id="appleid-signin" data-color="black" data-border="true" data-type="sign-in"></div>
  <script>
    const nonce = crypto.randomUUID();
    AppleID.auth.init({
      clientId: "com.example.shop.web",          // your Services ID
      scope: "email",
      redirectURI: "https://shop.example.com",    // a return URL you registered
      nonce: nonce,
      usePopup: true,
    });
    document.addEventListener("AppleIDSignInOnSuccess", (event) => {
      // event.detail.authorization.id_token is the Apple ID token
      exchangeForAccessToken(event.detail.authorization.id_token, nonce);
    });
  </script>
</body>
</html>

Use Apple's button as it is: Apple's Human Interface Guidelines allow only its black, white and outlined styles. In an iOS app, ASAuthorizationAppleIDProvider gives you the ID token (identityToken) the same way.

The name. Apple doesn't put the user's name in the ID token; it gives it to your app once, on the first sign-in only. ObjectsGrid takes the name from Google's ID tokens, but has none from Apple's: a User Account created with Apple gets a Person without a name. If you ask for it (scope: "name email"), save it yourself and set it on the Person with PATCH /persons/{id}.

3. Register the Services ID with ObjectsGrid

ObjectsGrid accepts an ID token only if it was issued to an app your Organization Account lists in its identityProviders. As an administrator, read your Organization Account and add an Apple entry with your Services ID and, for an iOS app, its bundle id, then replace it:

curl -X PUT https://apis.objectsgrid.com/organizationaccounts/<organization account id> \
  -H 'Content-Type: application/json' \
  -H 'X-ObjectsGrid-AccessPoint: EU' \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        "name": "Northpeak Outdoor",
        "accessPoint": "EU",
        "identityProviders": [
          {"issuer": "https://accounts.google.com", "clientIds": ["<your Google client id>"]},
          {"issuer": "https://appleid.apple.com", "clientIds": ["com.example.shop.web", "com.example.shop"]}
        ]
      }'

Send the whole account you read, with your change, and keep the apps already listed unless you want to stop them from signing in.

4. Exchange the ID token for an access token

The exchange is the same as for Google; send the nonce you gave Apple too:

async function exchangeForAccessToken(idToken, nonce) {
  const response = await fetch("https://apis.objectsgrid.com/oauth2/token", {
    method: "POST",
    headers: {"Content-Type": "application/json", "X-ObjectsGrid-AccessPoint": "EU"},
    body: JSON.stringify({
      grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
      subject_token: idToken,
      subject_token_type: "urn:ietf:params:oauth:token-type:id_token",
      client_id: "<organization account id>",
      nonce: nonce,
    }),
  });
  const { access_token, expires_in, refresh_token } = await response.json();
}

The response is 200 with an access token for one hour and a refresh token for three months. A 400 with "error": "invalid_grant" means the Services ID or bundle id isn't in your identityProviders, the ID token has expired, its email isn't verified, or its nonce isn't the one you sent.

Signing in with both Google and Apple

ObjectsGrid never joins accounts by email: a person who signed up with Google and then signs in with Apple, with the same address, is refused with 409, since another account already uses it; one who hid their email gets a second, separate account instead. To use both, the person signs in with the first provider and adds the second to their own account, with its ID token and their access token:

curl -X POST https://apis.objectsgrid.com/useraccounts/<user account id>/identities \
  -H 'Content-Type: application/json' \
  -H 'X-ObjectsGrid-AccessPoint: EU' \
  -H "Authorization: Bearer <their access token>" \
  -d '{"idToken": "<Apple ID token>", "nonce": "<the nonce>"}'

From then on, either sign-in opens the same account. See Authentication.

When a user deletes their account

If your app lets users delete their account, Apple requires it to revoke the user's Apple tokens too. Only your app holds them, so your app calls Apple's token revocation endpoint; ObjectsGrid keeps no Apple token.