In this tutorial you will learn about the Laravel 8 Firebase Phone Number OTP Auth Example and its application with practical example.
In this Laravel 8 Firebase Phone Number OTP Auth Example tutorial I will show you how to validate phone number using Firebase Phone Number OTP Auth In laravel. In this tutorial you will learn to implement firebase otp authentication for mobile or phone number verification in laravel 8. In this article I will share example to add mobile number OTP verification using firebase in laravel.
Laravel 8 Firebase Phone Number OTP Auth Example
In this step by step tutorial I will demonstrate you with example to add mobile number OTP verification using firebase in laravel. Please follow the instruction given below:
- Step 1 – Download Laravel 8 Application
- Step 2 – Connecting App to Database
- Step 3 – Create View
- Step 4 – Create Route
- Step 5 – Create Controller By Artisan Command
- Step 6 – Run Development Server
Before you start with this tutorial you need to visit Firebase Console and create a project. then you have to create web app on that project with phone number auth enabled in authentication tab.
Step 1 – Download Laravel 8 Application
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database-name DB_USERNAME=database-user-name DB_PASSWORD=database-password |
Step 3 – Create View
In this step we will create blade view file. Go to resources/views directory and create one blade view file index.blade.php. And then add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<html> <head> <title>Laravel 8 Phone Number Authentication using Firebase</title> <!-- CSS only --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <div class="container"> <h1>Laravel 8 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> <div class="card-body"> <div class="alert alert-success" id="sentSuccess" style="display: none;"></div> <form> <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> </form> </div> </div> <div class="card" style="margin-top: 10px"> <div class="card-header"> Enter Verification code </div> <div class="card-body"> <div class="alert alert-success" id="successRegsiter" style="display: none;"></div> <form> <input type="text" id="verificationCode" class="form-control" placeholder="Enter verification code"> <button type="button" class="btn btn-success" onclick="codeverify();">Verify code</button> </form> </div> </div> </div> <script src="https://www.gstatic.com/firebasejs/6.0.2/firebase.js"></script> <script> var firebaseConfig = { apiKey: "AIzaSyBPdVwUIYOY0qRr9kbIMTnxKpFw_nkneYk", authDomain: "itdemo-push-notification.firebaseapp.com", databaseURL: "https://itdemo-push-notification.firebaseio.com", projectId: "itdemo-push-notification", storageBucket: "itdemo-push-notification.appspot.com", messagingSenderId: "257055232313", appId: "1:257055232313:web:3f09127acdda7298dfd8e8", measurementId: "G-VMJ68DFLXL" }; firebase.initializeApp(firebaseConfig); </script> <script type="text/javascript"> window.onload=function () { render(); }; function render() { window.recaptchaVerifier=new firebase.auth.RecaptchaVerifier('recaptcha-container'); recaptchaVerifier.render(); } function phoneSendAuth() { var number = $("#number").val(); firebase.auth().signInWithPhoneNumber(number,window.recaptchaVerifier).then(function (confirmationResult) { window.confirmationResult=confirmationResult; coderesult=confirmationResult; console.log(coderesult); $("#sentSuccess").text("Message Sent Successfully."); $("#sentSuccess").show(); }).catch(function (error) { $("#error").text(error.message); $("#error").show(); }); } function codeverify() { var code = $("#verificationCode").val(); coderesult.confirm(code).then(function (result) { var user=result.user; console.log(user); $("#successRegsiter").text("you are register Successfully."); $("#successRegsiter").show(); }).catch(function (error) { $("#error").text(error.message); $("#error").show(); }); } </script> </body> </html> |
Step 4 – Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php 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 5 – Create Controller By Artisan Command
Now, lets create a controller named FirebaseController using command given below –
1 |
php artisan make:controller FirebaseController |
Now, go to app/http/controllers and open FirebaseController.php file. And put the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class FirebaseController extends Controller { /** * Write code on Method * * @return response() */ public function index() { return view('index'); } } |
Step 6 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/firebase-phone-authentication |