Lessons from Shin Chan

No matter how many years pass by, he will still remain the same 5-year-old kid. And for the past few years, he has been my stress buster and if there is one program on TV that I have watched often…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Authenticate the user of an android app by phone number using Firebase Authentication.

Have you ever wanted to authenticate a user of an android app using a phone number like WhatsApp and Viber do? You all know that these types of apps get user’s phone number, send them a verification code, then automatically verify and finally let him sign in to the app successfully. Don’t worry. Now you don’t need a high tech or SMS APIs for doing that. Firebase SDK will rescue you and help you to accomplish your goal easily and it is totally free of charge. This SDK will send a unique one time code to the given phone number as an SMS and then it will be validated. Firebase has a big range of authentication mechanisms like password authentication, email link authentication, authentication via google, facebook, twitter and lot more.

This document only focuses on describing how to implement a phone number sign-in flow using the Firebase SDK.

Here I have implemented a sample demo application. I will explain the implementation of phone number authentication using it. You can find the code base from,

For the implementation, I am using Android Studio 3.1.1. I assume you have the basics idea of Android and app creation. This post only targets to explain how to use firebase phone number authentication. One more thing to remember is that the phone number sign-in requires a physical device and won’t work on an emulator.

01) First, you have to create a new Android application using Android Studio. My app name is PhoneNumberAuthenticationDemo. The package name is demo.lakini.com.phonenumberauthenticationdemo. Remember these details. You will need them in the next step.

02) Add Firebase to the android application.For that we can use Firebase UI.

After that you have to Sign In to the Firebase to proceed.

03) Then you can add a project,by using Add Project button in the console.After filling all the details and agreements you can successfully create a project.

04) Then you will be redirected to the overview section in the console where you can see all the details about your newly created project.

Figure 3 : Project Overview

05) Now we are going to add our android app to this project.Then we can access all the functionalities of Firebase. For that click on Android logo in the top blue area(Add an app to get started).

Figure 4 : Add Firebase to Android app

In here, fill android package name with the same package name which you have used when creating app in Android studio. After that click Register app.

06) Get google-services.json and add to the app.

Figure 5 : Get google-services.json

You can download the json file from above UI. Copy and paste in your Android app module root directory as instructed in above figure.

Example(In my application) :

07) We can enable Phone number authentication also from Firebase UI.Refer Figure 3. Click Auth in the console or you can go to Authentication settings from left section of the page .

Figure 6 : Enable Phone authentication

Click Sign-in tab in Authentication .Then select phone from the sign -in provider.Then enable it.You can add test phone number for testing purposes from the same UI. Sometimes Firebase block phone numbers if you over use them from same device ID.So it is better to white list your phone number for testing in Firebase. There are some other advantages of white listing phone numbers.

Now we are going to start implementation of the app.The main UI will be like this.

Figure 7 : Number entering UI of the app
Figure 9 : Successful verification

First we need to add Firebase dependencies and google services dependencies to project build.gradle file.

Secondly, we need to add Firebase auth dependencies to your app-level build.gradle file as below.

Add Firebase dependencies under dependencies tag.For this implementation I have used country picker library called com.hbb20:ccp:2.2.2. So I added this dependency as well.You have to add google service and Firebase plugins to this file as well.

This is how to send a verification code to the phone number. This method will send a code to a given phone number as an SMS.

The below method is to verify the code.In here when you call PhoneAuthProvider.verifyPhoneNumber, you must also provide an instance of OnVerificationStateChangedCallbacks, which contains implementations of the callback functions that handle the results of the request.

In most apps, you implement the onVerificationCompleted, onVerificationFailed, and onCodeSent callbacks. You might also implement onCodeAutoRetrievalTimeOut, depending on your app's requirements.

onVerificationCompleted method is called in two situations:

After the code verified successfully, next step is to sign in the user. You can use the PhoneAuthCredential object that is passed to the callback.

After you get a PhoneAuthCredential object, whether in the onVerificationCompleted callback or by callingPhoneAuthProvider.getCredential, complete the sign-in flow by passing the PhoneAuthCredential object to FirebaseAuth.signInWithCredential as in below code snippet.

Hope this post will help you in your android development.For more information in this topic you can refer,

Add a comment

Related posts:

Reflection 5

In this first article, the author presents a very sobering the reflective commentary on the recent Pittsburgh synagogue shooting. The author is coming from a Jewish background and has some first-hand…

Fontus

I have long been inspired by the “metaverse” as described in Snow Crash by Neal Stephenson. In some of my previous experiments I had built grid based worlds where you could own a parcel of land and…

Advanced SEO Tips for Technical Documentation

Technical documentation should be optimized as any content to help your readers to find your documentation quickly and make your content more user-friendly. SEO is a great recourse of optimization…