An Introduction to Firebase Authentication

For those of you that are interested, Firebase has taken the liberty of making authentication as easy as three lines of code. As a quick reminder, this article is going to assume that you know how to integrate firebase to your app. Once you have taken the time of adding firebase, please remember that you have to make sure that your app has access to the authentication methods that we are going to be using. Please enjoy the following article on simple authentication.

The first part of authentication that you are going to have to get comfortable with is going be the firebase.auth().

For the most part, you are going to be working in this auth namespace that is going to return to you all the methods that you are going to need to sign up a user and log them in. While firebase allows you to authenticate in many different ways, this article is going to primarily focus on email and password.

The first method that we will need for user authentication is going to be auth.signInWithEmailAndPassword(email, password).

As you can see, the above method will be doing exactly what it says. It will attempt to sign in a user with an email and password passed in as its arguments. This method will then return to you a promise that will allow you to resolve the user. But what if the user doesn’t have an account yet? There has to be a complicated way to sign them up right?

It really is just as simple as that. The above method will create an account with the email and password passed in as its argument. Not only will this create the user account, but it will also sign them in. But again, because this is a promise it will resolve only once. What if you want to monitor when the user is logged in? Of course, there is a method for this as well.

The above method is going to take a call back that will be run whenever the state of the user changes. So, if a user logs in or out, the above method will run. If a user logs in, the user data will be held in the firebaseUser parameter. Once the user logs out, the value of firebaseUser will be null. As you can imagine, you will be able to perform some fun logic as the authenticated state changes within your application.

When a user gets created in your firebase store or database, you are going to also receive a couple of bonuses. Firebase will keep track of the following for you: the date the account was created and when the user last logged in. Firebase will also provide the following: an easy to use GUI that will allow you to manage those users, as well as a unique user id. All of this is provided for you but just running the signUpWithEmailAndPassword(email, password) method.

You will also be able to very easily set up email notification, and password reset emails without having to write any code at all. Not only is this an added security measure, but because it is so easy to implement, it isn’t any extra work for the developer to set this up. Firebase allows you to create a seamless user experience and it creates a seamless developer experience with easy to use methods.

As a reminder, remember to select how you are going to authenticate.

Under authentication in firebase, you can pick and choose how users can sign in. This can prove to be very useful since you can very easily choose a way that will also verify a users identity.

And that's all you need to get started on firebase authentication. With these three methods that you will get from firebase.auth(), you will be able to easily add authentication to your application.

Happy Learning!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store