How To Authenticate Users With Firebase In Swift

Oct 14, 2019 · 5 min read

Do you know how to create an API that manages a database and authenticates users?

If you are not a back-end developer, probably not. And that’s totally normal!

That’s not a problem with Firebase. Just add this library to your project and most of the work is done! You have a database to store whatever you want and authenticate users without creating a server and manage it. And it’s free.

This is perfect for small apps if you don’t know anything about back-end development. But, it’s also useful for bigger apps to reduce their costs.

Setting up Firebase

First of all, you need to create a new Firebase project.

To do that, you just need a basic Google account (if you have Gmail email address you can use it) then go to :

Here, click on Get started. You will be redirected to a page where you can add a new project and that’s what you need to do.

The first step is adding a name to your project. That’s a simple step, just type in whatever name you want to give to your project. Below the name, you can see your project ID. Copy that project ID, we are going to need it.

Then you can add Google Analytics to your project, just skip that part we don’t need that for this project and create the project.

Once the project is created, you are on the home screen of your project. From here, you can do everything like checking what’s stored in your database, add things to the database, change authentication rules…

Click on the iOS button and follow the steps by completing the form.

Then, you have a configuration file you need to add to your project. Download it and add it to your project by dragging the file to your Xcode project like it’s shown in the picture.

Then follow the steps to add the required pods. In our case just add:

Then follow the next step to add the configuration code in your project.

And that’s it, you’re done with the most complicated part of this tutorial!

Create users

Here is how you can create users :

And of course, the password is hashed for you automatically in the database, you can’t see it.

So don’t worry, passwords are stored safely.

But there is a problem. What if I want to add more than just an email and a password to my user? Like a name, an age, a gender… ?

Here is how you can do it :

You just get the currentUser (you just created one before so you can be sure that there is a current user logged in) and then you change a few properties and commit the changes.

Don’t forget to always handle errors as I did!

Signing in a user

After creating users, you need to allow them to log in!

Here is how you can do it:

Why do we have a weak self? And why do we create a strongSelf? The answers for all capture list questions are in my article about the difference between strong, weak and unowned in closures.

Now you must have a way to check if a user is logged in. And if a user is logged in, you need to get their profile data to view them.

There are 2 ways to check if a user is logged in:

  • The first is to receive a notification each time a user logs in to:
  • And the other way is to check if there is a current User as we have already done before:

As you have noticed, the main advantage of this method is that you can get the user’s profile at the same time as you check if a user is connected.

You can also see in the previous example how you can get the profile values of a user.

Of course, you have to handle errors with another block and you can’t force the unwrapping of values as I did!

Because if, for any reason, the values are nil, your application will crash immediately and you should always avoid taking such a risk in real applications!


I hope you enjoyed this article and you learned something!

There is so much more to say about Firebase I strongly recommend you check out its documentation!

If you didn’t like it, Amazon created its own database provider. It’s Amazon Cognito. Feel free to check out!

The Startup

Medium's largest active publication, followed by +569K people. Follow to join our community.


Written by


Freelance iOS developer

The Startup

Medium's largest active publication, followed by +569K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade