Ideas are easy. Implementation is hard. Be Passionate.

Firebase User Phone Authentication verification by otp in Android

As in my last tutorial, we understand the basic firebase concept like What is Firebase and How to use the Firebase features in our application? So I am not going into detail here about the basic understanding about the Firebase. To Use any feature of firebase we need to follow few basic simple steps to register my application.

Here are steps:

Please go to Firebase console to create the project. Here you can pass the project name and enter your country name etc.

You have to choose which platform do you want to use. Here we are configuring for Android platform, so I am choosing an option for Android app.

Now you need to enter your package name of your project. Ex: Here my package name is “com.sunil.firebase”. Other fields are optional for debug mode. But if you want to release your app that time you need to pass the SHA-1 key.

Once you have done your step.4 You will get downloaded one google-services.json file. You have to copy this file and paste into the app folder.

Great done well so far. Now you need to add Firebase dependencies to your Gradle file. Here please always check the latest version of the minimum crash and best feature.

1

2

compile'com.firebaseui:firebase-ui-auth:3.0.0'

compile'com.google.firebase:firebase-auth:11.4.2'

In my last tutorial, we already practiced the register and login with user email and password. I would be recommended to check the basic concept of Firebase about login and register with email and password then this post will help you to understand the concept Firebase User Authentication in android.

Now in this tutorial, we will earn how to login with a phone number to sending the OTP over users phone? For phone authentication of Firebase User, you need to enable the Phone Authenticate sign-in method from the firebase console.

Generally, Firebase phone Authentication can possible in two ways.

Firebase AuthUI method

Custom UI Phone Authentication

Firebase AuthUI method

Let’s discuss the first one where we can use the Firebase AuthUI which is provided by Firebase to Authenticate the user’s phone by OTP. Here Firebase AuthUI is provide everything for you to write the minimum line of code to authenticate phone. You need to write the Firebase AuthUI method call to authenticate and all other things will take care by Firebase. So this concept is pretty simple and easy to integrate our applications.

This minimum line of code will allow opening the Firebase Auth UI to enter the phone of the user to authenticate. Here You need to provide the correct information like country code and Phone number to authenticate. Now Firebase will start doing the job for you.

Now the second thing we need to take care the Firebase Authenticated result in onStartActvityResult. So let’s write the code for tracking the Firebase result.

Custom UI Phone Authentication

Let’s check how can we make this feature as custom, I mean, I don’t want the existing Firebase AuthUI feature. Basically, We want to use our app design for the same procedure. We can complete this task in basic three steps.

Enter Phone to get OTP (Firebase provides verification id and token)

Resend the opt or verification code if the otp did not get the provided on phone number.

Callback provides the three different methods to execute the task based on the firebase result. Here if the otp did not reach to provided phone number then you can call the resend the verification code again to your number. Let’s check the second steps.

Firebase will send the result to the same callback method. Now suppose you got the verification code on your phone number and now you need to verify this code with firebase API. It means we reached the third step. Let’s check the third step for opt verification by using the Firebase API.

Here User can authenticate with success and failure based on the API response. So we have done so for the custom UI for phone authentication for users. Here I divided code into three snippet part then it becomes difficult to understand for the beginner. Let’s share the whole Activity code together for easy life.

wrapping up: As we have seen that Firebase is awesome for instant building the app. Even if you do not have any backend ready till now then you can use the Firebase cloud for a backend to quick start your idea into the Android application. If you want to upload user profile while registering the user then Firebase provides the cloud storage to upload the file or image. Please check this post to upload a file on Firebase Storage in android. Here are the best offer and recommendation for you to download this.

Please do subscribe email to get all newsletters of this blog and if you feel that this post will help you to understand then do not forget to subscribe, share and comment below.

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Categories

For better assist

Hey Folks, Welcome to the MobologicPlus blog. I’m Sunil Gupta, a tech blogger from Bangalore, India.
I started MobologicPlus as a passion, by helping people to understand the technical kinds of stuff from this blog. You have some feedback for MobologicPlus. Where should I send it? Sure, drop us a line anytime at contact@mobologicplus.com