Laravel 10 Firebase Phone/Mobile Number OTP Auth Tutorial

Last Updated on by in Laravel

Laravel Mobile number OTP authentication tutorial; Throughout this step-by-step tutorial, you will find out how to create Phone or mobile number OTP authentication in Laravel using the Firebase Phone sign-in auth provider.

Firebase is a profound backend as a service tool, which makes the development work exorbitantly facile. To build the robust authentication system, it offers tons of ready-made features that you can implement in your app and amplify the development work.

In this tutorial, we are integrating the Firebase phone and mobile OTP authentication in the Laravel app.

To integrate Firebase mobile number OTP auth in Laravel app, you need to install a basic laravel app, connect it to the database, create a controller and bind it to the route.

Lastly, create a simple view for sending mobile number otp through the Firebase ecosystem.

So, without further ado, let us start implementing the firebase phone auth in the laravel app.

Laravel 10 Phone Number OTP Authentication using Firebase Example

  • Step 1: Install Laravel App
  • Step 2: Create Firebase Project
  • Step 3: Enable Firebase Phone Auth
  • Step 4: Add Database Details
  • Step 5: Create Controller
  • Step 6: Add Route
  • Step 7: Set Up Blade View
  • Step 8: Start Development Server

Install Laravel App

You need to commence the fundamental step by installing a new laravel application using composer command, ensure to configure composer on the system, then execute the following command.

composer create-project --prefer-dist laravel/laravel laravel-phone-otp-auth

Create Firebase Project

Head over to Firebase site, then signin using your firebase credentials, thereafter click on Create a project button.


Add project name, and click on continue.

Add firebase project

Next, add Firebase to your app, so to get credentials to click on the markup icon.

click on icon

Next, you need to register app and click on Next.

register app

From this page, you need to copy firebase configurations.

firebase configurations

Enable Firebase Phone Auth

The Firebase project has been created. Now you have to enable the firebase phone authentication, click on the authentication tab, head over to the Sign-in method section, click on the Phone item and enable the Phone auth.

Enable Phone Auth in Firebase

Add Database Details in ENV

Add database name, user name, and password into the .env file:


If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.


Create Controller

Open terminal, type command, then execute the command that generates a new controller; in this controller we will call the blade view for showing the mobile otp auth form.

php artisan make:controller PhoneAuthController

Open and add code in app/http/controllers/PhoneAuthController.php file:


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PhoneAuthController extends Controller

    public function index(){
      return view('welcome');

Add Route

Open and create a new route by using the PhoneAuthController, add the following code in routes/web.php:


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PhoneAuthController;

| Web Routes

Route::get('phone-auth', [PhoneAuthController::class, 'index']);

Set Up Blade View

We are using the default welcome blade view file to create the auth form’s locus, add bootstrap 5, firebase, and jQuery scripts in this template, add some custom functions that handle the phone auth in firebase.

Open and replace with the following code in resources/views/welcome.blade.php:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel 8 Phone Number OTP Auth Example</title>
    <link href="" rel="stylesheet">

    <div class="container mt-5" style="max-width: 550px">

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

        <h3>Add Phone Number</h3>

        <div class="alert alert-success" id="successAuth" 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-primary mt-3" onclick="sendOTP();">Send OTP</button>

        <div class="mb-5 mt-5">
            <h3>Add verification code</h3>

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

                <input type="text" id="verification" class="form-control" placeholder="Verification code">
                <button type="button" class="btn btn-danger mt-3" onclick="verify()">Verify code</button>

    <script src=""></script>
    <!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
    <script src=""></script>

        var firebaseConfig = {
            apiKey: "API_KEY",
            authDomain: "",
            databaseURL: "",
            projectId: "PROJECT_ID",
            storageBucket: "",
            messagingSenderId: "SENDER_ID",
            appId: "APP_ID"
    <script type="text/javascript">
        window.onload = function () {

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

        function sendOTP() {
            var number = $("#number").val();
            firebase.auth().signInWithPhoneNumber(number, window.recaptchaVerifier).then(function (confirmationResult) {
                window.confirmationResult = confirmationResult;
                coderesult = confirmationResult;
                $("#successAuth").text("Message sent");
            }).catch(function (error) {

        function verify() {
            var code = $("#verification").val();
            coderesult.confirm(code).then(function (result) {
                var user = result.user;
                $("#successOtpAuth").text("Auth is successful");
            }).catch(function (error) {


Start Development Server

You may quickly test the application; just type the suggested command on the terminal, execute it start the development server.

php artisan serve

Use the following url to send the otp to authenticate using the phone number via firebase.

Laravel Phone Number OTP Authentication using Firebase


We have explained how to authenticate with mobile number OTP in Laravel with Firebase. It is easy to add firebase authentication to login into a user by sending a one-time SMS message to the user’s mobile number.