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,
https://github.com/Lakini/Android_Demo_Samples/tree/master/PhoneNumberAuthenticationDemo
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.
https://console.firebase.google.com/u/0/
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.
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).
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.
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) :
<App Location>/PhoneNumberAuthenticationDemo/app
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 .
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.
- Test phone number authentication without consuming your usage quota.
- Test phone number authentication without sending an actual SMS message.
- Run consecutive tests with the same phone number without getting throttled. This minimizes the risk of rejection during App store review process if the reviewer happens to use the same phone number for testing.
- Test readily in development environments without any additional effort, such as the ability to develop in an iOS simulator or an Android emulator without Google Play Services.
- Write integration tests without being blocked by security checks normally applied on real phone numbers in a production environment.
Now we are going to start implementation of the app.The main UI will be like this.
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:
- Instant verification: in some cases the phone number can be instantly verified without needing to send or enter a verification code.
- Auto-retrieval: on some devices, Google Play services can automatically detect the incoming verification SMS and perform verification without user action. (This capability might be unavailable with some carriers.)
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,