Sign In with Apple with Flutter & Firebase Authentication

aditya nandedkar
5 min readDec 28, 2021

--

In this tutorial we’ll see how to add Apple Sign In to our Flutter & Firebase apps from scratch.

We will use the the_apple_sign_in Flutter plugin available on pub.dev.

This plugin makes it possible to support Apple Sign In on iOS, and enable it as an authentication method on Firebase.

However, note that this plugin supports iOS only, not Android, and you can only use this on devices running iOS 13 and above.

Some context

Apple Sign In is a new authentication method that is available on iOS 13 and above.

It is very convenient, as your iOS users already have an Apple ID, and can use it to sign in with your app.

So just as you would offer Google Sign In on Android, it makes sense to offer Apple Sign In on iOS.

Prerequisites

  • Xcode 11 installed
  • An Apple Developer Account
  • An iOS 13.x device or simulator, signed in with an Apple ID

Project Setup

After creating a new Flutter project, add the following dependencies to your pubspec.yaml file:

Note: we will use Provider for dependency injection, but you can use something else if you prefer.

Next, we need add Firebase to our Flutter app. Follow this guide for how to do this:

After we have followed the required steps, the GoogleService-Info.plist file should be added to our Xcode project.

And while in Xcode 11, select the Signing & Capabilities tab, and add “Sign In With Apple” as a new Capability:

Adding Sign In With Apple capability

Once this is done, ensure to select a team on the Code Signing section:

Set Code Signing options

This will generate and configure an App ID in the “Certificates, Identifiers & Profiles” section of the Apple Developer portal. If you don’t do this, sign-in won’t work.

As a last step, we need to enable Apple Sign In in Firebase. This can be done under Authentication -> Sign-in method:

This completes the setup for Apple Sign In, and we can dive into the code.

Checking if Apple Sign In is available

Before we add the UI code, let’s write a simple class to check if Apple Sign In is available (remember, we’re supporting iOS 13+ only):

Then, in our main.dart file, let's modify the entry point:

The first line prevents an exception that occurs if we attempt to send messages across the platform channels before the binding is initialized. This is needed because we’re calling Firebase.initializeApp() before running the app.

Then, we check if Apple Sign In is available by using the class we just created.

And we use Provider to make this available as a value to all widgets in our app.

Note: this check is done upfront so that appleSignInAvailable is available synchronously to the entire widget tree. This way we don't need a FutureBuilder to perform this check in our widgets.

Adding the UI code

Instead of the default counter app, we want to show a Sign In Page with a button:

Back to our main.dart file, we can update our root widget to use the SignInPage:

At this stage, we can run the app on an iOS 13 simulator and get the following:

Apple Sign In Button

Adding the authentication code

Here is the full authentication service that we will use to sign in with Apple (explained below):

First, we pass a List<Scope> argument to our method. Scopes are the kinds of contact information that can be requested from the user (email and fullName).

Then, we make a call to AppleSignIn.performRequests and await for the result.

Finally, we parse the result with a switch statement. The three possible cases are authorized, error and cancelled.

Authorized

If the request was authorized, we create an OAuthProvider credential with the identityToken and authorizationCode we received.

We then pass this to _firebaseAuth.signInWithCredential(), and get a UserCredential that we can use to extract the FirebaseUser.

And if we requested the full name, we can update the profile information of the FirebaseUser object with the fullName from the Apple ID credential.

Note: According to this issue, the Apple sign-in APIs always return null for the fullName data after the initial login. After that, the only information that is returned is the uid. Hence the code above has some defensive null checks before updating the Firebase display name.

Error or Cancelled

If authentication failed or was cancelled by the user, we throw a PlatformException that can be handled by at the calling site.

Using the authentication code

Now that our auth service is ready, we can add it to our app via Provider like so:

Then, in our SignInPage, we can add a method to sign-in and handle any errors:

Finally, we remember to call this on the callback of the AppleSignInButton:

Testing things

Our implementation is complete, and we can run the app.

If we press the sign-in button and an Apple ID is not configured on our simulator or device, we will get the following:

Apple Sign In Settings prompt

After signing in with our Apple ID, we can try again, and we will get this:

After continuing, we are prompted to enter the password for our Apple ID (or use touch ID/face ID if enabled on the device). If we have requested full name and email access, the user will have a chance edit the name, and choose to share or hide the email:

After confirming this, the sign-in is complete and the app is authenticated with Firebase.

Note: if the sign-in screen is not dismissed after authenticating, it’s likely because you forgot to set the team in the code signing options in Xcode.

Congratulations, you have now enabled Apple Sign In in your Flutter app! Your iOS users are grateful. 🙏

Show some love ❤️ if you find it useful !!

Thank You ! Keep Coding !

For more information, casual chat, tips and tricks on Flutter follow
https://www.instagram.com/_thecodingninja_/

--

--

aditya nandedkar

A passionate Flutter developer learning new things daily !