1st Sep 2021

Phone number OTP verification with Firebase

Mobile App

Written By, Parth Davda

Overview

In this era of mobile application development, Authentication is a basic need of every application in the market, there are many types of authentication like Facebook login, email login, phone number OTP verification available using firebase in android. In this blog, we will learn phone numbers with OTP verification with firebase. With firebase phone number authentication use will get OTP(one-time password) through SMS to sign in.

Connect your app to firebase

First of all, click on tools in the top app bar of your android studio and then click on firebase. Then you can see a new window open on the right side of the studio. Then click on authentication and then click on authentication using google sign-in.so, your app is connecting to firebase and your project will be seen in firebase.

Enable Phone Authentication in firebase.

After successfully connecting your app with firebase, open your project in the console then go to the authentication on your left side window in the console. Then click on the sign-in method. Then click on the phone and enable it.And last save it.

enable-authentication

Add Dependencies to your Project.

After enabling the phone in the firebase console. Make sure to add the following dependencies in your build.gradle file. After adding dependencies sync your project.

Design your XML related to your Project.

Now, let’s start with the basic design with two edit text and with two buttons. First, edit text for the Enter Mobile Number and first button for generate OTP. Second edit text for the Enter Received OTP and second button for the verify OTP.

Send verification code to the user.

After completing the design. When the user enters a number and clicks on send OTP then the below code is useful for sending verification code. So In the Kotlin file, the code is as per below.

In the above code, there are basically three callbacks. OnVerificationCompleted, OnVerificationFailed, OnCodeSent.

  1. onVerificationCompleted : This method is called when the user is verified without the use of code automatically or Code is null and verified on its own.
  2. onVerificationFailed : This method is called when the verification fails due to any reason like phone number format error or etc.
  3. onCodeSent : This method is called when the  SMS is sent to the entered number.

Verify OTP and Sign-in.

Now, For verifying the OTP user can match the send OTP with the entered OTP which is entered by the user in edit text. So for verifying bellow code is used.

Limitations of OTP in firebase:

From the use of firebase OTP verification using a phone number is FREE. The free plan of firebase has Ten Thousand Verification per month, but if you exceed this limit, you need to pay for that.

Conclusion

These steps and the whole approach are helpful for many users to do authentication in an easy way and also easy to understand.
All the source code is available Firebase Otp Authentication  at this GitHub Repo.

Thanks for reading this blog. I hope this is useful to you all.

Written By,

Android Developer at Yudiz Solutions Pvt. Ltd