Use Phone Number Authentication using Firebase in Android

That’s when you can create hundreds of app with real-time user validation…

.. ..you will be unstoppable.

And today I have something that will help you to start using firebase:

Phone Number Authentication is a firebase guide that you can use right now.

Phone Number Authentication using Firebase

Firebase Auth provides a secure authentication system that allows the users to sign in or sign up for your application.

This enhances user experience and felt more secure.

Usually creating an app with a sign in, compatibility is much more complex than it looks.

In addition, to sign in, there are many different users flows, such as resetting passwords, creating new accounts, disable accounts, then manage multiple accounts and this list goes on.

This can be a ton of work to do for a small team.

Luckily! Google worked with Fabric and bring different of authentication features in Firebase platform. As a result, now you can use email and phone authentication as well as federated identity through major websites such as Google, Facebook, Twitter.

One thing I love about Google Firebase that It’s completely free till 10,000 authenticate users and it does not have any limit on the number of SMS can be sent. Of course, it has a limit on how many SMS can be sent on a single phone number within a time period.

How Does Phone Number Authentication work?

Firebase Phone Number Authentication offers to authenticate user phone number and decreases the risk of a fake sign in or sign up. This authentication method ensures that the user has physical access to that cell phone and the phone number is correct.

What is FirebaseUI?

FirebaseUI is a collection of open-source libraries that offers commonly used UI elements. Those elements can easily be added and customised to fit with your app theme.

That’s why we will use FirebaseUI so we do not have everything from scratch. You just have to integrate FirebaseUI and you will get a prebuilt sign in layouts for Phone Number Authentication and commonly used social platforms like Google, Facebook, Twitter.

Note: Before I even start this tutorial you will need to build and test this to a device with a sim card that can receive SMS and have Google Play services 10.2.x or higher install.

Let’s start

Here I’m going to break the whole tutorial into parts so you can understand clearly.

Create a Project

First of all, we have to create a new project and named it as “TestPhoneAuth”. Then click next.

After that check “Phone and Tablet”, as Phone Number Authentication is meant to run on API level 16 or higher. For the purpose of this tutorial, you have to choose API level 16.

Finally, choose “Empty Activity” and press next. This can take some time when it’s done you are ready to go next step.

Configure Firebase Phone Authentication

There are different methods to connect an android app to Firebase, this is the simplest method I think. In this method, we will use Firebase assistant, which is an inbuilt tool for Android Studio.

Just go to Tools->Firebase.

After that, you will see an Assistant in the top right, left click on that a window will appear.

Click on Authentication, select Email and password authentication.

First of all select (1) Connect your app to Firebase, click on connect.

NOTE: If you are not signed in on Android Studio already, you need to sign in.

Select Create new Firebase project if it’s not already selected. Then click on connect to Firebase it will connect your project to the database.

Now select (2) Add Firebase Authentication to your app, click on set up. Once you enabled this option it will Sync build.gradle file.

Adding Fabric repositories

After that, you have to add Fabric repositories in your project level build.gradle. To do so follow this Android-> Gradle Scripts->build.gradle(Project:TestPhoneAuth).

Then add following,

maven { url 'https://maven.fabric.io/public' }

Adding FirebaseUI

Now you have to add FirebaseUI dependency to your app level build.gradle. To do follow this Android-> Gradle Scripts->build.gradle(Module: app).

When adding google play services or firebase-auth dependencies always check that version supported by used firebase-UI version or not. I found an awesome chart provided by samtstern.

Enable phone number authentication firebase

Once you are on Authentication page click on the Sign-in method, select phone and enable and save it.

It will enable phone number authentication firebase for this app. You can check the default template of SMS verification under TEMPLATES.

Creating Activity

In the purpose of this tutorial, you have to create two activity. PhoneNoAuth activity will take the phone number from the user and verify it and SuccessActivity activity will display successfully login message.

So, create two activities

PhoneNoAuth

SuccessActivity

If you do not know how to create activity follow the bellow image.

Right click on the first directory under java then select New-> Java class. After that give the class name and click on ok. That’s all you just created an activity.

AndroidManifest.xml

Now you have included those activities in your app mainfests. You will find it under app, double-click on this file to open it. Change you AndroidManifest according to below code but do not edit anything on your manifest package.

styles.xml

I do not create any custom style so this file will look same as yours. In this file, two themes are there. One theme is created by default called AppTheme, which contains three colours. You can customize the theme as you wish.

exit.xml

Create a new directory under res and named it as transition. Under the transition directory create exit.xml file and add following.

<?xml version="1.0" encoding="utf-8"?>
<explode
/>

Read More: How much an average App Developer can earn??

Download

You can check and download this project from my GitHub repository. If you modified this project then share it with me I will happy to watch it.

Conclusion

I tried to point out every possible point and to keep this article easy to understand. In this purpose, I do not include other stuff like custom respond time, or resend pin. If you want to know about those you can check here and feel free to ask me in the comment section if you face any problem.

And that’s it hopefully this tutorial will help you to understand phone number authentication firebase android, If you like this tutorial share this with other developers out there.

If you face any problems, write it down in the comment section, I will solve your issue.