Phone Number Verification using Firebase in Android
Phone number Firebase Authentication is used to sign in a user by sending an SMS message to the user’s phone. The user signs in using a one-time code contained in the SMS message. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.
Note: To implement this in Java language refer to this article Firebase Authentication with Phone Number OTP in Android
Step by Step Implementation
Step 1: Create a new project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Connect Project to Firebase.
Step 3: Add dependency to the build.gradle file and click “sync now”
Step 4: Create two new activities.
Create two new activities. One PhoneNumberActivity.kt with the layout file activity_phone_number.xml for entering the phone number and starting the authentication process. Second OtpActivity.kt with the layout file activity_otp.xml for entering OTP received from firebase.
Step 5: Working with layout
Working with activity_phone_number.xml. Go to the activity_phone_number.xml file and write the following code.
Working with activity_otp.xml. Go to the activity_otp.xml file and write the following code.
Working with activity_main.xml. Go to the activity_main.xml file and write the following code. This is the final activity where we reach after verification is completed.
Step 6: Working with the PhoneNumberActivity.kt file
Go to the PhoneNumberActivity.kt file and refer to the following code. Below is the code for the PhoneNumberActivity.kt file. Comments are added inside the code to understand the code in more detail.
Step 7: Working with the OtpActivity.kt file
Go to the OtpActivity.kt file and refer to the following code. Below is the code for the OtpActivity.kt file. Comments are added inside the code to understand the code in more detail.
Step 8: Go to AndroidManifest.xml and add the following code
Step 9: Enable the Android DeviceCheck API for your project
In the Google Cloud Console, enable the Android DeviceCheck API for your project. The default Firebase API Key will be used and needs to be allowed to access the DeviceCheck API.
Step 10: Add SHA keys from Android Studio to Firebase
Copy SHA1 and SHA-256 keys from your project and paste them to the firebase console. Below is the screenshot to guide you.
Note: Please refer to this article How to Generate SHA1, MD5, and SHA-256 Keys in Android Studio?
On the Firebase console go to Project Overview -> Project Setting, and click add fingerprint button, and add your SHA keys copied from firebase
Step 11: Enable Phone Number sign-in for your Firebase project
In the Firebase console choose your project, open the Authentication section. On the Sign-in Method page, enable the Phone Number sign-in method.
Github repo here.
Please Login to comment...