Saturday, 30 June 2018

Firebase Phone Authentication Android Tutorial

Firebase Phone Authentication Android Tutorial

Today we will learn about Firebase Phone Authentication. Firebase is fantastic, and it provides almost the features that you need for the backend of your application. So in this tutorial, we will learn about Firebase Phone Authentication, and I am pretty sure that you have already seen this thing in many apps. You enter your phone number, receives an OTP, and then you use that OTP to authenticate.


Benefits of using Phone Authentication

When you use phone number authentication, you have many benefits like
  • Preventing Fake Users: When you use phone authentication, the user can’t be able to register for multiple accounts, as for each account a unique phone number is needed.
  • Increase User Value: When you have all the users verified by a phone number the value of your user base increases.
  • Increased Security and User Experience:Nowadays more people using apps and remembering passwords are a headache for many users, so they end up using weak passwords. Using phone authentication increases security and user experience, as the user does not need to create and remember passwords, they will enter their number and then they can receive a temporary authentication code by SMS.

Why Firebase Phone Authentication?

For implementing phone authentication you need to pay for SMS service, but with firebase, you can do it for FREE, isn’t it awesome? The free plan of firebase has Ten Thousand Verification per month.  Thats enough for the starter apps I guess, but yes if you exceed this limit, you need to pay.
So, guys enough for the discussion now let’s start our project and let’s see how we can integrate phone authentication in our application.

Firebase Phone Authentication Tutorial

Now let’s learn how to implement Firebase Phone Authentication in our project.

Creating a new Android Studio Project

  • Again we will do it in a new project, so create a new Android Studio project. In my case I have created a project named FirebasePhoneAuthentication.
  • Once the project is completely loaded, we will add firebase authentication into it.

Adding Firebase Authentication

  • Click on tools -> firebase. It will open an assistant from where you can add all the firebase services into your project.
  • So from here you can create a new firebase project or you can also select an existing project.
  • Connect your android project to firebase project and add the dependencies using this assistant.
If you are confused about this step please follow this Firebase Cloud Messaging Tutorialwhere I explained all the steps about adding firebase to android project. 

Enable Firebase Phone Authentication

  • To do this go to Firebase Console and open the project that you are using.
  • Then go to Sign In Method and enable Phone Authentication.

Designing Screens

Before moving into designing I would like to tell you that, for Sign In we will have two activities, in the first activity the user will enter his phone number and in the next we will verify the code sent by SMS.

Enter Phone Number Screen

  • I have created this design for this Screen.
  • For designing the above screen come inside activity_main.xml and write the following xml code.
  • The above xml file will give you some errors as I have used some drawable resources, but don’t worry you can get the resources in my source code and the link of the source code is at the bottom, so keep reading. 

Verify Phone Number Screen

Now we will create the next activity where we will very the phone number. In this screen as well we need an EditText where user will input the code. But we will detect the SMS automatically so user do not need to enter manually.
  • First create a new Activity in the project, and I have created an activity named VerifyPhoneActivity.







  • For the above design for your VerifyPhoneActivity, open the activity_verify_phone.xml and write the following xml code.
  • Now we will create one more activity named ProfileActivity.

Creating ProfileActivity

  • Create a new EmptyActivity named ProfileActivity and this activity will open after the successful login.
  • In this screen we have nothing but only a TextView with a welcome message, as you can see below.




Getting the Mobile Number

  • We will get the mobile number of the user in MainActivity. So come inside MainActivity.java and write the following code.
  • The above code is very straightforward. We are just taking the mobile number from EditText and sending it to VerifyPhoneActivity.class with intent.

Verifying Mobile Number

Sending Verification Code

  • For sending verification code we will use the following code.
  • sendVerificationCode(): This method will send the verification code. You can also see that I am concatenating +91 with the mobile number which is the country code for India. But in real scenario you might have users from different countries. So if that is the case, you need to let the user select their country as well so that you know the code, and you can also  tell the user to input the number with country code.
  • mCallbacks: This is our callback that will help us to know the code is sent or not. It has three methods.
    • onCodeSent(): This is called when the code is sent successfully.  The first parameter here is our verification id that is sent. So we are storing it in our mVerificationId object.
    • onVerificationFailed(): This method is called when the verification is failed for some reasons, so here we are only displaying a simple toast.
    • onVerificationCompleted(): This method is called when the verification is completed. Here we have the PhoneAuthCredential Object which will give us the code if it is automatically detected.

Verifying Code and Sign In

  • To verify verification code we will use this method. If the verification is successful we will let the user sign in into the application.








  • Thats it, it will work. Now let me also show you the full code for VerifyPhoneActivity.java.

VerifyPhoneActivity Code


  • Here is the full code of VerifyPhoneActivity.java I have also written comments to explain. But if you have any confusion, you can leave your comment below.


  • Thats it now you can try running your application.

Firebase Phone Authentication Source Code

  • Still having troubles? Don’t worry you can get my source code. Just unlock the link by subscribing to my youtube channel.
Firebase Phone Authentication Tutorial Source Code
So that’s all for this Firebase Phone Authentication Tutorial friends. I hope you found it useful, and if you really did then please help me by sharing this post will all your friends who are learning Android Application Development.
And for any question you can leave your comments here. Thank You ðŸ™‚

No comments:

Post a Comment