Dev48
Language
  • About
  • Services
  • Industries
  • Technologies
  • Articles
  • Contacts
Book a call
    Home/Articles/Integrating two factor authentication in laravel with vonage
Dev48
info@dev48.org+375 29 320-40-96WhatsApp@isky_by

© 2026 · All rights reserved.

Integrating Two-Factor Authentication in Laravel with Vonage

Source: Vonage API Developer•November 25, 2026 at 02:28 PM

Integrating Two-Factor Authentication in Laravel with Vonage

Learn how to add SMS two-factor authentication to a Laravel application using the Vonage Verify API, Laravel boilerplate code, and a sprinkling of AI.

Introduction

One of Laravel's strengths in recent times is what others might call a weakness, though it's all based on preference.

The boilerplate starter kit options that Laravel ships with give developers (and, with Laravel Boost, MCP guidelines for agents) plenty of functionality with the structure they provide.

One of the least contentious features is the scaffolding for end-user authentication, known as Laravel Fortify.

Surely, nine times out of ten, whatever you are building is going to need users, a login system, and some sort of auth mechanism and session storage. I've always wondered why every framework, regardless of language, doesn't have this option.

Because it comes out of the box, we're going to install Laravel, run everything needed to get the bootstrap up, and then add SMS two-factor authentication to the login process using the Vonage Verify API.

With Laravel Boost, I'll walk you through how Claude Code implements this feature. While I've built this functionality many times before, in the current climate of asking an agent to do it for you, you can save plenty of time here. This also serves as a practical guide for agents parsing this very article on Laravel best practices.

The Flow

We're essentially going to interrupt the login flow by introducing a new Two-Factor Middleware, not by editing Fortify's authentication mechanism. Because this middleware extends the auth behavior rather than refactoring it, we don't need to touch the existing code.

Prerequisites

  • PHP 8.4+

PHP 8.4+

  • The Laravel Installer

The Laravel Installer

  • A web server, such as Laravel Herd

A web server, such as Laravel Herd

  • A Vonage account

A Vonage account

Installation

Using the Laravel Installer, create a new application:

The Laravel CLI will now ask you questions to get up and running as quickly as possible.

Answer "yes" to using a starter kit, and choose the Livewire scaffold, which is more straightforward than needing a full JavaScript frontend.

Enable Laravel's built-in authentication, which in our case will use a SQLite file with baked-in migrations. Select single-file Livewire components, and skip team support.

The Laravel Installer will now run Composer's create-project command.

Vonage Credentials

Our recommended authentication method for the Vonage Verify API is to create an application on your Vonage dashboard and download a private.key file, which should be moved to the project root directory.

  • To create an application, go to the Create an Application page on the Vonage Dashboard, and define a Name for your Application.

To create an application, go to the Create an Application page on the Vonage Dashboard, and define a Name for your Application.

  • If you intend to use an API that uses Webhooks, you will need a private key. Click “Generate public and private key”, your download should start automatically. Store it securely; this key cannot be re-downloaded if lost. It will follow the naming convention private_<your app id>.key. This key can now be used to authenticate API calls. Note: Your key will not work until your application is saved.

If you intend to use an API that uses Webhooks, you will need a private key. Click “Generate public and private key”, your download should start automatically. Store it securely; this key cannot be re-downloaded if lost. It will follow the naming convention private_<your app id>.key. This key can now be used to authenticate API calls. Note: Your key will not work until your application is saved.

  • Choose the capabilities you need (e.g., Voice, Messages, RTC, etc.) and provide the required webhooks (e.g., event URLs, answer URLs, or inbound message URLs). These will be described in the tutorial.

Choose the capabilities you need (e.g., Voice, Messages, RTC, etc.) and provide the required webhooks (e.g., event URLs, answer URLs, or inbound message URLs). These will be described in the tutorial.

  • To save and deploy, click "Generate new application" to finalize the setup. Your application is now ready to use with Vonage APIs.

To save and deploy, click "Generate new application" to finalize the setup. Your application is now ready to use with Vonage APIs.

Your .env file will need the following information:

Configuring the Vonage Client

There are two parts to this: ensuring the two environment variables above are available to the Laravel application config files, and creating a service provider so a controller can use Dependency Injection to automatically wire in a configured Vonage PHP Client.

First, create config/vonage.php and populate it:

Laravel magically scans the config directory and parses the return values under the filename namespace. To get the Application ID, you would now call config('vonage.application_id') from anywhere in your app. It's important that the config sits here and env() is only called here, as Laravel caches environment variables. This has led to much head-scratching when testing a local feature.

Next, bind the Vonage Client into the service container. Open app/Providers/AppServiceProvider.php and register the client as a singleton in register():

Now, every time Client is type-hinted into a class, the app service container will give you a pre-configured client back.

Adding the Phone Number

When registering a new user, the form should allow adding a cellular phone number for 2FA, which will be stored in the Users database table. Tedious things like database migrations and form modifications are exceptionally trivial for AI, so I prompted Claude:

A quick look at the modifications, and everything looks good.

First, a new migration:

Handy that it's addedunique(): we can't have conflicts with numbers for authentication. It's also added phone_number into the fillable attribute in the Model (something I always remember to do last):

In the template for Fortify, it's added a new field for the phone number:

The last step is to make sure the number is persisted on new user creation, so Fortify's CreateNewUser now looks like this:

Binding the Elements: Creating the Middleware

I've done this manually, but it could easily be a prompt. Create the middleware in the Laravel CLI:

Head into the new middleware and add the check:

The successful 2FA process completes in two parts: it checks that the user authentication (password) has worked, then checks that the session has completed 2FA via a custom flag (we'll add that in the code-entry route).

Now, in bootstrap/app.php, register the middleware:

Routing and Controller

There are three things to modify in the app routing:

  • Add a GET route to display the code-check page (after the controller has sent a code to the user).

Add a GET route to display the code-check page (after the controller has sent a code to the user).

  • Add a POST route for the user to enter and submit the code for verification.

Add a POST route for the user to enter and submit the code for verification.

  • Add the newly registered two-factor-code middleware to the Dashboard landing page.

Add the newly registered two-factor-code middleware to the Dashboard landing page.

Both the GET and POST routes sit under the auth middleware. Inroutes/web.php, add these three routes:

We've already named the controller in the routing above, so create it in the console:

Our controller does two things: it either renders the code page (the middleware has already sent the SMS) or checks the code. If the code check succeeds, it adds a verified flag to the user's session. Because it's a session and not a cookie, this is something that can't be spoofed or edited client-side. Add the logic:

The Last Step: Adding the Code Entry View

Everything is in place now except the view to render the code-input part of the login process. As it's a Blade template, this is fairly trivial for Claude or your AI model of choice to code up. I prompted:

With models advancing and Laravel Boost (and Vonage's MCP Server) giving more and more skills for code generation, it's astonishing how little you need to put into the prompt. I only gave the file name as a precaution: it has the context anyway to name it correctly.

The output matches the rest of Laravel's boilerplate designs:

We're all wired up. Time to boot up the app and see what we've got.

One login attempt later, after creating an account:

And finally, putting the SMS code in, the login process is completed:

A nice-looking default dashboard. 2FA with Vonage, completed.

Delving Deeper and Conclusion

This was very much the "route-one" for the tutorial.

SMS verification alone shouldn't be considered entirely secure (spoofing is possible, for instance), but it should be part of a suite of security tools.

For production, I would allow users to select their method of 2FA. Given that the Verify API supports multiple delivery channels such as email and WhatsApp, users should be given the option to select these.

This basic implementation also only completes one verified session per user. Realistically, you could track the number of days since a verification was completed, then add some logic (a month or a week, depending on your security auditing requirements) and restart the process.

Security is built in layers: if you want to see more of Vonage's security options, check out our Network-Powered API solutions.

← All articles

What this article says

More in Telecom & Networks

All →
What is PII masking? Techniques and best practices
Infobip

What is PII masking? Techniques and best practices

Tech Mahindra and Mahindra University Bring Together Financial Services GCC Leaders to Explore AI-Led Innovation
Tech Mahindra

Tech Mahindra and Mahindra University Bring Together Financial Services GCC Leaders to Explore AI-Led Innovation

Ciberseguridad empresarial: proteger la continuidad del negocio ante los ciberataques
Telefónica

Ciberseguridad empresarial: proteger la continuidad del negocio ante los ciberataques

Beyond connectivity: Building financial technology that impacts millions
Ericsson

Beyond connectivity: Building financial technology that impacts millions

「My au」がau Starlink Direct SOSに対応
KDDI

「My au」がau Starlink Direct SOSに対応

Connectivity opens the door. Skills decide who walks through it
Ericsson

Connectivity opens the door. Skills decide who walks through it

More from Vonage API Platform

How to Receive SMS Delivery Receipts with ASP.NET Core MVC
Vonage API Platform

How to Receive SMS Delivery Receipts with ASP.NET Core MVC

Automated Insurance Claims in JavaScript With AI Damage Detection
Vonage API Platform

Automated Insurance Claims in JavaScript With AI Damage Detection

Build an ASP.NET MVC App to Send SMS Messages
Vonage API Platform

Build an ASP.NET MVC App to Send SMS Messages

Introducing the Redesigned Video Inspector: Now in Public Beta
Vonage API Platform

Introducing the Redesigned Video Inspector: Now in Public Beta

Introducing Version 1.6 of the Vonage Video Reference App
Vonage API Platform

Introducing Version 1.6 of the Vonage Video Reference App

Vonage Video API 2.36: Retry Logic, Observability, and iOS 27 Support
Vonage API Platform

Vonage Video API 2.36: Retry Logic, Observability, and iOS 27 Support