Firebase Android Series : Authentication

Welcome to the first article of Firebase Android Series. Here we are going to learn how to build an Android app using Firebase from scratch..
This is the first article of Firebase Android Series. A series of articles focused on learn how to use Firebase in any aspect that you would need to implement it as part of your published applications, or your start implementing it in your new ones.
The first module that we are going to see is Firebase Authentication.
Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.
Firebase Authentication integrates tightly with other Firebase services, and it leverages industry standards like OAuth 2.0 and OpenID Connect, so it can be easily integrated with your custom backend.
It would usually take you months to set up and maintain your own authentication system, but Firebase Authentication lets you build an authentication service in a few lines of code that will handle everything for you.
We are going to create an authentication service to allow our users register in our application and start using it. Let’s get to work.
Configuring the project
First of all. We need to create our project in Firebase, you can learn how to do it here.
Now that our project has been created, we are going to start configuring it to use the Authentication service. First, go to the Authentication tab in the Firebase console. There we are going to click on Sign-in Method and enable the providers that we need for our application: Email/Password and Google.

For some authentication providers such as Google, we need to add our SHA-1 key from on the Android project in Project overview section:

You can check how to retrieve your SHA-1 key on this link
Now that we have our project properly configured for our authentication service, let’s jump to the code. We are going to focus on the most used cases when using an authentication service.
Creating an account
There are multiple ways to create accounts using Firebase Authentication, which can be summed up in the following sections
Firebase APIs uses Google Play Tasks API to manage all their calls. If you are not familiar with the Tasks API you can give it a look here.
- Email and password
We can create a user in Firebase by just using an email address and a password. When our task completes. We will receive a FirebaseUser
as a result. A FirebaseUser
provides us with some information already managed for Firebase. Such as the associated providers to the user, the user creation timestamp or the last login date.
- External providers
Creating an account with an external provider is similar to our last code. The only difference is that we need an AuthCredential
object created from an external provider(Google, Facebook, Github…). Each provider has different APIs. Study and check the provider’s API before decide if it’s worth using an authentication provider in your app.
Now that we have our AuthCredential
we can create our user on Firebase.
This code can looks huge but I’ll highlight the relevant points. When can’t create an account directly with a provider. The method to Login and create an account is the same when using AuthCredentials
. For that reason we should manage it in case that we want a different behavior.
In the case above, what I’m doing is fetch the providers associated to the given email and check if a provider with the same name is already registered. Depending if the provider exists or not, we can implement our own custom logic.
If we follow the happy case(where our user doesn’t have the provider associated), we will receive an AuthResult
item. This item contains a FirebaseUser
and AdditionalUserInfo
. Classes that will provide us with information that we could need for application logic.
Note: When an user sign-in using Google provider. The parameter isEmailVerified
will be always true. There is no need to manage our user verification. Google already does it for us.
Sending a verification email
We already know how to create a user. Now we will verify our users to avoid no desired users in our app. To achieve this we are going to send a verification email. Firebase already provides us this functionality through the method sendVerificationEmail()
.
Note: If you want to customize the template of the email sent by Firebase you can check how to do here.
Using the code above, we already sent an email to our user. The next step is verify it. We can access to our current FirebaseUser
through our AuthInstance
. The given user will have the emailVerified
parameter that we need. But this user instance is not updated automatically.
To update our FirebaseUser
we need to make use of the method reload
. Let’s give a look to our application verifyUser
method:
Note: In the same way that reload()
is needed to successfully execute some methods. There are exceptional cases when we want to execute sensitive operations in which Firebase will force us to re-authenticate our users. Such as delete an user or change the email provider. For this special cases, you must use the methods reauthenticate()
or reauthenticateAndRetrieveData().
Login an user
Our application already allow our users to sign-in and verify themselves. Now it’s time to allow them to login in our application with their credentials. We are going to support login our users with an email and a password an using Google as an external provider. The code is similar to the one used above to create an account.
- Email and password
Like createUserWithEmailAndPassword()
we receive an AuthResult
as a result. We will use it to manage the user data with our custom logic and redirect the user to the home activity.
- External providers
Like I explained above. A login can be made in the same way than creating an account. We just need to take into account if an account asw already created or not.
Keep an user logged
To track the authentication status of our user. Firebase provides us with an AuthStateListener
that receive callbacks each time that an user authentication status changes. This allow us to track in our application lifetime the user status and implement our custom logic. Such as kick the user back to the login screen if the user is signed out or redirect the user to the home screen directly from the splash of the application.
Let’s give a look to the code:
In the same way that this listener is attached, it can be removed using removeAuthStateListener
.
Finally we can sign out our users manually calling AuthInstance.signOut()
.
Summarizing
- Create users with an email and a password with
createUserWithEmailAndPassword()
and log in them withsingInWithEmailAndPassword()
. - Create or login users with an external provider we use
singInWithCredentials()
. - Check the providers associated to an email with
fetchSignInMethodsForEmail()
. - Send verification emails with
sendVerificationEmail()()
and check the changes on your updatedFirebaseUser
instance with the parameterisEmailVerified
. - Use
reload
to update yourAuthInstance
andreauthenticate()
to allow the user to proceed with any sensitive operation. - Track your user authentication status with
AuthStateListener
.
Github Sample
This series will always work over the same project. Building a chat application using Firebase and Kotlin. You will find each one of the different articles code in individual branches of the project.