Signing in users with Apple

This document shows you how to use Identity Platform to add Sign in with Apple to your web app.

Before you begin

Configuring your app with Apple

On the Apple Developer site:

  1. Follow the steps in Configure Sign in with Apple for the web. This includes:

    1. Registering a Return URL, which looks like:

      https://project-id.firebaseapp.com/__/auth/handler
      
    2. Temporarily hosting a file at the following URL to verify your domain:

      https://project-id.firebaseapp.com/.well-known/apple-developer-domain-association.txt
      

    Additionally, take note of your Services ID and Apple team ID — you'll need them in the next section.

  2. Create a Sign In with Apple private key. You'll need the key and its ID in the next section.

  3. If you use Identity Platform to send emails to your users, Configure your project with Apple's private email relay service using the following email:

    noreply@project-id.firebaseapp.com
    

    You can also use a custom email template, if your app has one.

Complying with Apple's anonymized data requirements

Apple gives users the option of anonymizing their data, including their email address. Apple assigns users who select this option an obfuscated email address with the domain privaterelay.appleid.com.

Your app must comply with any applicable developer policies or terms from Apple regarding anonymized Apple IDs. This includes obtaining user consent before associating any personally identifying information (PII) with an anonymized Apple ID. Actions that involve PII include, but are not limited to:

  • Linking an email address to an anonymized Apple ID, or vice versa.
  • Linking a phone number to an anonymized Apple ID, or vice versa
  • Linking a non-anonymous social credential, such as Facebook or Google, to to anonymized Apple ID, or vice versa.

For more information, refer to the Apple Developer Program License Agreement for your Apple developer account.

Configuring Apple as a provider

To configure Apple as an identity provider:

  1. Go to the Identity Providers page in the Cloud Console.

    Go to the Identity Providers page

  2. Click Add A Provider.

  3. Select Apple from the list.

  4. Under Platform, select Web.

  5. Enter your Services ID, Apple team ID, Key ID, and private key.

  6. Register your app's domains by clicking Add Domain under Authorized Domains. For development purposes, localhost is already enabled by default.

  7. Under Configure your application, click Setup Details. Copy the snippet into your app's code to initialize the Identity Platform Client SDK.

  8. Click Save.

Signing in users with the Client SDK

To sign in a user:

  1. Create an instance of the OAuthProvider provider object, using the ID apple.com:

    JavaScript

    var provider = new firebase.auth.OAuthProvider('apple.com');
    
  2. Optional: Add OAuth scopes. Scopes specify what data you are requesting from Apple. More sensitive data may require specific scopes. By default, when One account per email address is enabled, Identity Platform requests the email and name scopes.

    JavaScript

    provider.addScope('email');
    
  3. Optional: Localize the authentication flow. You can specify a language, or use the device's default language:

    JavaScript

    firebase.auth().languageCode = 'fr_FR';
    // To apply the default browser preference instead of
    // explicitly setting it.
    // firebase.auth().useDeviceLanguage();
    
  4. Use the provider object to sign in the user. You can either open a pop-up window, or redirect the current page. Redirecting is easier for users on mobile devices.

    To show a pop-up, call signInWithPopup():

    JavaScript

    firebase
      .auth()
      .signInWithPopup(provider)
      .then(function(result) {
        // The signed-in user info.
        var user = result.user;
         // You can also get the Apple OAuth Access and ID Tokens.
        var accessToken = result.credential.accessToken;
        var idToken = result.credential.idToken;
    
        // ...
      })
      .catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
      });
    

    To redirect the page, first call signInWithRedirect():

    JavaScript

    firebase.auth().signInWithRedirect(provider);
    

    Then, call getRedirectResult() to retrieve the Apple token when your page loads:

    JavaScript

    // Result from Redirect auth flow.
    firebase
      .auth()
      .getRedirectResult()
      .then(function(result) {
        if (result.credential) {
          // You can get the Apple OAuth Access and ID Tokens.
          var accessToken = result.credential.accessToken;
          var idToken = result.credential.idToken;
    
          // ...
        }
        // The signed-in user info.
        var user = result.user;
      })
      .catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
    
        // ...
      });
    

Unlike many other identity providers, Apple does not provide a photo URL.

If a user chooses not to share their real email with your app, Apple provisions a unique email address for that user to share instead. This email takes the form xyz@privaterelay.appleid.com. If you configured the private email relay service, Apple forwards emails sent to the anonymized address to the user's real email address.

Apple only shares user information, such as display names, with apps the first time a user signs in. In most cases, Identity Platform stores this data, which lets you fetch it using firebase.auth().currentUser.displayName during future sessions. However, if you allowed users to sign into your app using Apple before integrating with Identity Platform, user information is not available.

What's next