Sign in with Apple was announced in 2019 and allows users to authenticate into your Firebase app with an Apple ID. Users must have two-factor authentication (2FA) enabled on their Apple account AND be signed into iCloud.
The following lesson demonstrates how to configure Apple SignIn with the Firebase JavaScript SDK.
Follow the steps outlined below to implement Sign In with Apple in your app.
You must be a member of the Apple Developer Program. It costs $99 per year.
From the Apple Developer Portal go to Certificates, Identifiers & Profiles » Identifiers. Create a new App ID or update an existing app and give it the Sign In with Apple capability.
Make sure Sign In with Apple is Enabled
From Certificates, Identifiers & Profiles » Identifiers, create a new Service ID and make sure is linked to your primary App ID. Configure it point to your Firebase hosting URL.
Replace the project ID for your domain
Go to Service ID you created in the previous step and click configure, then click download. Save the file in your web app’s public hosting directory under ./well-known/
.
Deploy this file to your domain so Apple can verify it.
firebase deploy --only hosting
You should see a green checkmark next to the domain
From Certificates, Identifiers & Profiles » Identifiers, create and download a new private key - keep it private. It is used to validate requests from Apple with your Firebase project.
Download the private key. Do not expose it publicly.
Head over the to the Firebase Console and go to the Authentication » Sign-in Method tab. Enable Apple and enter the required details.
Copy the contents of the private key in the console
We now have all the pieces in place to implement SignIn with Apple into our web app. Because this is Firebase, it only requires a few lines of code. Assuming you have Firebase installed in your project, simply make a reference the the provider and call signInWithPopup()
.
SignIn with Apple popup seen by the end-user
The frontend implementation is an async function that can bound to a button click to trigger the popup modal. Make sure to follow Apple UI guidelines when designing the button.
const auth = firebase.auth();
async signInWithApple() {
const provider = new firebase.auth.OAuthProvider('apple.com');
const result = await auth.signInWithPopup(provider);
console.log(result.user); // logged-in Apple user
}
#firebase #security #web-app #web-development