Laravel 8 Two Factor Authentication with SMS

In this tutorial you will learn about the Laravel 8 Two Factor Authentication with SMS and its application with practical example.

In this Laravel 8 Two Factor Authentication with SMS tutorial I will show you how to implement Two Factor Authentication with SMS in laravel 8. In this tutorial you will learn to authenticate user using SMS Two Factor Authentication in laravel. In this article I will share example to implement two factor authentication in laravel application. We will be using twilio api to send sms for two factor authentication. In two factor authentication using sms we will be sending sms to validate user account in laravel.

Laravel 8 Two Factor Authentication with SMS

In this step by step two factor authentication tutorial I will demonstrate you with example on how to add sms two factor authentication in laravel. Please follow the instruction given below:

Step 1 : Install Laravel

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Step 2: Install Twilio Configuration

First of all you need to create account on twilio.com and add phone number. Then you will be provided account SID, Token and Number. Now, you need to add account SID, Token and Number in .env file:

.env

Now you need to install twilio/sdk composer package to use twilio api. Use the following command to install twilio/sdk.

Step 3: Create Migration for table

Now, in this step we will create migration file. Please run the following command:

Once above command is executed there will be a migration file created inside database/migrations/ directory, just open migration file and update the function up() method as following:

Now, run the migration to create database table using following artisan command:

Step 4: Create and Update Models

Next we will create a model file.

app/Models/User.php

app/Models/UserCode.php

Step 5: Create Auth Scaffold

Use the following command to generate laravel auth scaffolding:

Lets create auth scaffold:

Step 6: Create Middleware

Now we will create a custom middleware to implement two factor authentication.

app/Http/Middleware/Check2FA.php

app/Http/Kernel.php

Step 7: Create Route

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

Step 8: Create and Update Controllers

Now, lets open RegisterController and LoginController and put the following code in it:

app/Http/Controllers/Auth/RegisterController.php

app/Http/Controllers/Auth/LoginController.php

Let’s create TwoFAController controller and put the following code in it.

app/Http/Controllers/TwoFAController.php

Step 9: Create and Update Blade File

Finally we will update default register blade file and create new 2fa blade file as following:

resources/views/auth/register.blade.php

resources/views/2fa.blade.php

Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the Laravel 8 Two Factor Authentication with SMS and its application with practical example. I hope you will like this tutorial.