Phone Authentication in Flutter with Firebase

Fatih Özgür
Codimis
Published in
4 min readDec 5, 2022

We can say that the authentication method with the phone differs from other authentication methods in that it is strong. It prevents some forms of identity theft and unwanted attacks by making sure that a user cannot use different accounts.

What is OTP?

One-time password (OTP) is an automatically generated password that authenticates a user. An OTP is more secure than a static password.

In this article, I will tell you how to set up this verification method using Firebase. You just have to follow the steps. I hope you will like it.

Step 1: Adding Packages

After creating your project, we must add the necessary packages. Packages to be added are listed below.

  1. firebase_auth
  2. firebase_core
  3. fluttertoast (optional)

Step 2: Firebase Project Creation

At this stage, we need to open a project on Firebase. Simply follow the steps below.

Go to the Firebase site. Sign in with your Google account.

Create project.

Click on the Android icon.

In the folder where your project is located, under defaultConfig under android>app, write the name of Package in this field opposite applicationID.

Download the google-services.json file and locate it under android>app.

In this step, you need to add the necessary plugins to your build.gradle file under android>app.

Now continue by making the necessary additions to your build.gradle file under gradle.

Step 3: Preparing the Application

At this stage, we will prepare the pages and the functions that will work on them. The first page we'll work on is the login page. On this page, a number will be obtained from the user, and a field will be opened where he can enter the OTP code if he receives an SMS to the number.

For more detailed information, you can take a look at Firebase’s documentation.

The second page is the onboard page to be switched after login. This place can be completely according to your wishes. After the user is logged in, there will be an option to log out.

We need to initialize Firebase in main.dart file.

Step 4: Adding Fingerprint

You need to create fingerprint on your computer. You can use the code below.

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android 

Note: If you have JDK on your computer, the keytool is installed. Or you can visit here to install JDK.

Go to the settings of your Firebase project. In the “Add Fingerprint” option in the “Your Apps” section, enter the SHA1 and SHA256 values respectively

Step 5: Authentication Activation

Click “Authentication” at the bottom of the Project Overview page. After clicking the “Get Started” button, activate the “Phone” option among the methods that appear.

The project is finished at this stage. It can redirect the user to the reCaptcha page for spam protection. If you don’t want this to happen, I can recommend that you activate Android Device Verification API for your project via Google Cloud Console.

Note: Note that when you enable this API, it will only work on phones with Google Play Services.

Thank you for reading the Medium post I shared. If you want to read the articles that I will share in the future, you can follow me.

--

--

Fatih Özgür
Codimis
Writer for

I am a student studying computer engineering. I am a developer in creating and designing digital experience. Currently, I’m focusing on making fullstack apps.