Laravel Phone Number Verification using Firebase Example

By Hardik Savani March 13, 2021 Category : Laravel


This article will give you example of laravel firebase phone auth. we will help you to give example of laravel mobile number verification firebase auth. i would like to show you laravel firebase mobile otp. it's simple example of firebase phone authentication laravel example. Let's get started with laravel phone number verification with firebase.

you can easily use this example with laravel 6, laravel 7 and laravel 8 version.

In this tutorial, i will create step by step simple example of firebase phone auth in laravel. we will create firebase app and give login with phone enable. then we will write simple code to mobile verification(opt) in laravel.

let's see bellow preview and step to complete mobile verification:


Step 1: Create Firebase Project and App

In first step, we have to go Firebase Console and create a project. then you have to create web app on that project as like i added bellow screenshot:

After given name and next then you will received firebase sdk as like bellow screen shot:

Next you need to enable phone number auth from bellow link:


You have to save that all information because we will use in our app.

Step 2: Install Laravel

first of all we need to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blogFirebase

Step 3: Create Route

Here, we need to add one route with FirebaseController controller so let's add that route in web.php file.



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\FirebaseController;



| Web Routes



| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!



Route::get('firebase-phone-authentication', [FirebaseController::class, 'index']);

Step 4: Create Controller

we will create new controller as like beloow

now, so let's add like as bellow:



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FirebaseController extends Controller



* Write code on Method


* @return response()


public function index()


return view('firebase');



Step 5: Create Blade File

let's create new blade file that where we write all logic on phone auth.




<title>Laravel Phone Number Authentication using Firebase -</title>

<!-- CSS only -->

<link href="" rel="stylesheet">

<script src=""></script>



<div class="container">

<h1>Laravel Phone Number Authentication using Firebase -</h1>

<div class="alert alert-danger" id="error" style="display: none;"></div>

<div class="card">

<div class="card-header">

Enter Phone Number


<div class="card-body">

<div class="alert alert-success" id="sentSuccess" style="display: none;"></div>


<label>Phone Number:</label>

<input type="text" id="number" class="form-control" placeholder="+91********">

<div id="recaptcha-container"></div>

<button type="button" class="btn btn-success" onclick="phoneSendAuth();">SendCode</button>




<div class="card" style="margin-top: 10px">

<div class="card-header">

Enter Verification code


<div class="card-body">

<div class="alert alert-success" id="successRegsiter" style="display: none;"></div>


<input type="text" id="verificationCode" class="form-control" placeholder="Enter verification code">

<button type="button" class="btn btn-success" onclick="codeverify();">Verify code</button>





<script src=""></script>


var firebaseConfig = {

apiKey: "AIzaSyBPdVwUIYOY0qRr9kbIMTnxKpFw_nkneYk",

authDomain: "",

databaseURL: "",

projectId: "itdemo-push-notification",

storageBucket: "",

messagingSenderId: "257055232313",

appId: "1:257055232313:web:3f09127acdda7298dfd8e8",

measurementId: "G-VMJ68DFLXL"




<script type="text/javascript">

window.onload=function () {



function render() {

window.recaptchaVerifier=new firebase.auth.RecaptchaVerifier('recaptcha-container');



function phoneSendAuth() {

var number = $("#number").val();

firebase.auth().signInWithPhoneNumber(number,window.recaptchaVerifier).then(function (confirmationResult) {




$("#sentSuccess").text("Message Sent Successfully.");


}).catch(function (error) {





function codeverify() {

var code = $("#verificationCode").val();

coderesult.confirm(code).then(function (result) {

var user=result.user;


$("#successRegsiter").text("you are register Successfully.");


}).catch(function (error) {








Ok, now we are ready to run.

So let's run project using this command:

php artisan serve

I hope it can help you...

Tags :