Sign In With Apple Using Firebase Authentication
In this tutorial, we will learn how to sign up a new user using “Sign In With Apple” and register the data inside Firebase Firestore. Let’s get started!
Difficulty: Beginner | Easy | Normal | Challenging
Environment : Xcode 12 & UIKit ( programmatically)
Prerequisites:
- Project running on iOS 13 or later
- Having Firebase Auth and Firebase Firestore SDK installed
Set Up Firebase & Apple Sign In
First of all, you need create a new project on Firebase and install the Firebase SDK to your project, follow my tutorial and get back to this page after if you haven’t done it.
Now, we still need to do some seup on both Xcode and Firebase to work with Sign In With Apple.
Go to Xcode, go to your main target > Sign and capabilities > “+ capabilities” > Select “Sign In With Apple”.
Get back to your Firebase console and enable Sign In With Apple.
Firebase Console > Authentification > Start > Sign-In method > Apple > activate > Register.
Also, set up the project to work with Firebase Firestore, so we will be able to store our user data later on.
Cloud Firestore > Create database > start in test mode > Activate.
After these steps completed, run your app and you should be there: your app is running, Firebase is connected, Authentification with Apple in Firebase Authentification method is enabled and the sign-in with Apple capability is activated on Xcode.
All right, we are done setting up Firebase and Sign In With Apple. Now let’s code!
Go to your ViewController file and import the following framework at the top:
Declare the ASAuthorizationAppleIDButton right over the viewDidLoad method (choose the style and type that you want):
Now let’s set up our button at the bottom of our screen, copy/paste this code right after the viewDidLoad closing graph.
Also, call the setupappleButton function inside the viewDidLoad and you will see this beautiful button appears on the screen 📱
Now, we are ready to add an action to this button (this code is largely based on the Firebase documentation)!
Let’s go ahead, copy/paste the following code inside the ViewController file:
Now, let’s give the action to the startSignInWithAppleFlow, replace the current function with this one:
Now, you will see Xcode warning you because we didn’t conform to ASAuthorizationControllerDelegate and ASAuthorizationControllerPresentationContextProviding protocols yet, copy/paste the code below after the last graph of your code in ViewController (it’s an extension).
This code allows us to connect to the Apple Server, which will return a unique token in Firebase, check if the user already exists and if it doesn’t, register the user into our database (Firebase Firestore) with three elements: an email, a name, and a user ID.
Run your app and sign in with Apple!
After completing the sign-in process (better with a real device, the simulator often doesn’t work), get back to your Firebase console, you will see this:
Currently, Apple only shares the email with apps the first time a user signs in using sign in With Apple.
We have done it! We successfully sign up with “sign in with Apple” and register the data into Firestore with name, email and the user identifier ✔️
If you have any questions, leave me a comment and if you find it useful, share it with someone who might also find it interesting! If you lose some pieces, find the complete code of the ViewController below 😉
Thanks for reading! I’m always happy to have a chat and collaborate at hello@sullivandecarli.com. Consider subscribing to enjoy unlimited access to my articles and all of Medium through my referral link.