Firebase Authentication using iOS SDK

The ultimate guide for implementing Firebase Authentication using Swift

Dejan Atanasov
Swift2Go
7 min readAug 6, 2018

--

About Firebase

Firebase is a Google-backed platform that is becoming very popular amongst mobile developers. It offers plenty of functionalities for mobile developers to implement inside their apps like crash reporting, analytics, databases, push notifications, chat, cloud storage and many many more. For example, you can build the backend for your app without even managing any infrastructure.

Firebase was created to simplify the mobile app development process and in the same time speed it up. You will now see how easy and fast it is to implement the authentication feature inside your app.

In this tutorial, I will cover the following points:

  1. Installation
  2. Setup
  3. Register
  4. Login
  5. Verify Email
  6. Reset Password
  7. Update User Details

#1 Installation

We will start by installing the necessary dependencies via CocoaPods. Just add the below code into your Podfile and run pod install in Terminal.

#2 Setup

After we have successfully installed the dependencies that we need, it’s time to set up a Firebase project. Visit the Firebase Console and create a new project. Inside the project, you will see a sidebar with many functionalities. We need to focus on the Authentication tab. 👇

Upon clicking on the Authentication tab, you will be asked to choose a Sign-In Method. Select the Email/Password method and enable it by using the switch. This will enable your app for using this type of authentication.

Note that we will only cover the Email/Password method as it is the most common one. Once you get the idea how it works, you can implement any of the other methods available.

Configure FirebaseApp

Let’s do some coding now… Open AppDelegate and import Firebase at the top of the file. Configure a Firebase App shared instance by adding FirebaseApp.configure() inside the application:didFinishLaunchingWithOptions: method.

GoogleService-Info.plist

The last step would be to locate the GoogleService-Info.plist which contains all the project configuration and add it inside your project. When generating the .plist file, please make sure that the bundle id is matching with yours. The .plist file can be downloaded from Settings -> Project Settings.

Run the project, and if everything is fine we can proceed to the next step. If you are facing some issues, please follow this link for more detailed info about the setup.

#3 Register

In order to test the other methods, we have to start by creating a user. At the top of your file please import FirebaseAuth , import FirebaseDatabase and import FirebaseStorage. These 3 classes will be needed during the whole tutorial.

The createUser() method is a wrapper of the FirebaseAuth’s createUser() method. This method expects a valid email and password, otherwise, it will return an error. The errors are very descriptive and you will know what is wrong right away.

If the callback doesn’t contain an error, you should find the user object under Auth.auth().currentUser. The currentUser is an instance of FIRUser.

You can check if the user has been successfully created directly from the console. All the users are stored under the Authentication tab. 👇

#4 Login

Assuming that the Register method worked, it is time to learn how to Login to the app.

If you compare the Register and Login methods, you will see how easy Firebase made it for us developers. The structure of the methods is the same, and the only difference is the naming. The login() method is also a wrapper of the FirebaseAuth’s signIn() method.

If the callback doesn’t contain an error, you should find the user object under Auth.auth().currentUser.

Sign Out

I assume that your app will need a Sign Out feature. In order to sign out the currentUser you must call the following method. The signOut() method will destroy the Auth.auth().currentUser.

#5 Verify Email

The user object will work fine without verifying the user’s email address. You don’t have to implement this part, but it’s always good to know if the email belongs to the user or not.

By calling this method, Firebase will send an email to the user with a verification link included. Upon pressing the link, the user successfully verifies his email address. You can check if the user has verified by calling Auth.auth().currentUser?.isEmailVerified.

Reloading User Details

In order to get the latest user information, we must reload the currentUser object. 👇

For example, if the user verifies the email and you don’t reload the user, you will still see the isEmailVerified as false. To update the currentUser object we must call the reload() method.

#6 Reset Password

The process for resetting a password is very similar to the email verification process.

By calling the sendPasswordReset() method together with the email that the user has entered, Firebase sends the user a password reset link. The user is redirected to a page to enter the new password and that’s it. Firebase handles the whole flow for you.

#7 Update User Details

If you have noticed, the Register method creates a user with only an email populated. But, there are more properties to the currentUser instance that we can populate after that. For this tutorial, I will show you how to add a Display Name and a Profile Picture.

Create a Profile Change Request

The currentUser instance contains a method called createProfileChangeRequest(). That method creates a FIRUserProfileChangeRequest instance, that we can use to update the currentUser properties. I have written the below method for easier updates of the user object.

This method checks for changes in the displayName or photoURL properties and then by calling commitChanges() it updates the user with the latest values that were passed to the request.

Now, you might wonder how to get the photoURL or how to change the displayName. Don’t worry, I have prepared methods for that as well.

Update Profile Picture and Display Name

To update the display name or the profile picture of the user, you will have to call the following method. I have prepared this method to update (1) only the display name, (2)only the profile picture or (3)both.

Update Display Name
For updating the display name we have to send a new value to the name attribute and it will get updated. It’s quite simple.

Update Profile Picture
Updating the profile picture is a bit more complex. The FIRUserProfileChangeRequest accepts a photoURL which is of a type URL. So, the first thing we need to do is create that URL.

In order to create the URL, we must use FirebaseStorage which creates a reference and uploads the photos under the path we provide for that reference. The profileImgReference that I have created above, consists of a child named “profile_pictures” and also the ID of the currentUser.

In other words, by calling profileImgReference.putData() we are uploading the image under the following URL: /profile_pictures/user.id. If the image is successfully uploaded to the path, a callback is returned. Inside the callback, we need to trigger another profileImgReference method called downloadURL() which will return us the photoURL.

We now have the photoURL, and we are ready to pass it to the createProfileChangeRequest() method where it will commit the changes.

That’s it from this tutorial and if it helped you please 👏 or share this story so others like you can find it. Thank you for your attention! 🚀

Check out my latest projects:

Read more of my writing on Medium:

Subscribe to my Newsletter:

--

--

Dejan Atanasov
Swift2Go

Senior iOS & Android Developer | Creator of Swift2Go | @hackernoon writer | Hire Me: http://bit.ly/2XfxbBR