User Authentication Using Phone Number – Firebase

Any application is successful as long as they have strong user base. User might install your app or even can navigate through all of your public pages but when it comes to join. I, as a User don’t want to fill out multi option form to get my self registered. User needs very simple process. Android in itself is a smart OS so are the users and a smart user do things in smart way. As technology evolved, they introduced registration through mobile number. Today we will see that how firebase suppose to be a life savior while implementing user authentication using phone number.

First and foremost we need to do is to create a new android project and connect this project to the firebase console. To do so, get to the firebase console and add a new project and follow the instruction to configure the project. While in the process you would get a google-services.json file. Put this file into your app folder.Then in that project add authentication. From authentication tab go to the sign in option, This is where we will see lots of sign in option. We just need to enable phone and configure their recommended settings.

firebase sign in option, phone authentication, firebaseUI liberary,orange camino
firebase sign in option

To Connect your app with the firebase we would need SHA key. The way i prefer to get one is using android studio gradle console. Click on gradle->app->android->signingReport and in your gradle console you would see the SHA key.

Things to do post configuration

So far we are only trying to set up firebase to work with our application. Now lets play a bit with the code. We need to add gradle dependency in our project and app level gradle files and sync the project.
Once we are done with the configuration then get back to our android code. Here we will be adding dependency script in gradle files. FirebaseUI dependency will take care the authentication process from showing sigin option on your activity view to maintaining session or password recovery. Our activity will be a normal empty activity. We need to write few lines of code to initialize the firebaseauh and firebaseauth UI.
At project level gradle file add classpath ‘com.google.gms:google-services:3.1.0’ in the dependency block then Add maven {
url “https://maven.google.com”
in repositories area.

Firebase configuration - gradle dependecy
Firebase configuration – gradle dependecy

In our app level gradle file add apply plugin: ‘com.google.gms.google-services’  at the bottom of the file then add compile ‘com.firebaseui:firebase-ui:2.4.0’ For everything else that we are supposed to achieve here.

In our activity class add this piece of code inside oncreate method.

// Here we will be initializing the firebase
mAuth = FirebaseAuth.getInstance();
// we will first check whether the user is already verified or not
if (mAuth.getCurrentUser() != null) {
// user already signed in Do whatever your application is required to do
} else {
// Here we will list down all the signin option we need to show
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(
Arrays.asList(
new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build()
))
.build(),
RC_SIGN_IN);

}

finally check the activity result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_SIGN_IN)
{
Log.d(TAG,”user authenticated”);
}else{
Log.d(TAG,”user not authenticated”);
}
}

That was all. Comment down your experience while setting up phone number authentication using firebase.