Firebase Phone Number Authentication In Android Studio

Firebase Phone Number Authentication In Android Studio

Firebase Phone Number Authentication In Android Studio.Hello Guys, Welcome to my new tutorial. Today we will learn how we can implement Phone Number Authentication/Verification using Firebase. Firebase is platform which allow to build web and mobile applications without server side programming language. It provides almost every features ( Authentication,Push Messaging Services,Real Time Database,Storage etc) which we need for our application.

You have seen Phone Number Authentication in most of the application. In which you enter your Phone Number and it send a OTP(One time password) to your number and you need to enter that OTP to authenticate.

Firebase provides simple way to authenticate user by using Phone Number Authentication and yet it is simple to implement. So lets start.

Previously we have already seen how we can implement this feature using Facebook Account Kit. You can check out that tutorial here – Facebook Account Kit SMS Authentication for Android

Firebase Phone Number Authentication In Android Studio – Video Demo

Firebase Phone Number Authentication In Android Studio

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

Creating Android Project

  • Create a new project in Android Studio File–>New–>New Project.
  • Enter Application Name, Package Name, Select Empty Activity and click on finish.

Add Firebase To Our Project

  • Click on tools -> firebase.

Add Firebase To Project- Firebase Phone Number Authentication

  1. Connect to Firebase – Click on Connect to Firebase and then you need to choose project you can also create new firebase project and press Connect to Firebase.
  2. Add Firebase Authentication to your app – Next you need to add gradle to your project. Click “Add Firebase Authentication to your app” and Accept Changes.

Add Firebase To Project- Firebase Phone Number Authentication

  • 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.

Enable Phone Number Authentication Firebase

Designing UI (Firebase Phone Number Authentication In Android Studio)

  • Now lets Design the User Interface. In our project we will have two activity which are MainActivity and AuthActivity.
  • In Main Activity we will check whether user is login or not if he is not we will redirect him to Auth Activity.
  • Create AuthActivity.class and activity_auth.xml.

activity_main.xml-

activity_auth.xml-

Add Firebase To Project- Firebase Phone Number Authentication

Back End Code

  • First let us check whether user is logged In or not.
  • In our MainActivity.java first declare an Instance of FirebasAuth.

  • After that In onCreate() method , initialize the FirebaseAuth instance.

  • When initializing your Activity, check to see if the user is currently Logged in.If not redirect him to Auth Activity.

  • We need to add one more function in our this activity which is LOGOUT. We will use Button to logout user.
  • Complete code for MainActivity.java is-

Send a verification code to the user’s phone

  • For sending verification code we will use the following code when user will press Send Code button in AuthActivity.java.

  • 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 signInWithPhoneAuthCredential() method. If the verification is successful we will let the user sign in into the application.
  • Firebase phone authentication have two features if user is using same number on the device it will auto detect the code and verify but if user want to verify other number which he is using on other phone he needs enter manually for that we are using one button.
  • Complete code for AuthActivity.java is-

  • That’s it now run your application and enjoy.
  • I have tried to my best to explain if you have any doubts feel free to comment below or mail me at csesumeet@gmail.com

Firebase Phone Number Authentication In Android Studio

Download Complete Project

Firebase Phone Number Authentication Complete Project

Subscribe To Our YouTube Channel

 

Like Us On Facebook

 

Other Tutorials-

RECENT POST

2 Comments

  1. Sir, Help Me
    When I try to run application as shown in your tutorial the application make bug.
    And show me RuntimeExcutation Error. I’m mention the hole report in the down :-

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jdgames.aum/com.jdgames.aum.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.jdgames.aum. Make sure to call FirebaseApp.initializeApp(Context) first.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6238)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
    Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.jdgames.aum. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.4:240)
    at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
    at com.jdgames.aum.MainActivity.onCreate(MainActivity.java:39)
    at android.app.Activity.performCreate(Activity.java:6868)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)
    … 9 more

    This is the hole bug report.
    I use all of your codes.

Leave a Reply

Your email address will not be published. Required fields are marked *