DEV Community

Sesha
Sesha

Posted on

5 2

Easy way to Generate and Validate OTP in the Laravel

In this post I will show how to generate and validate the OTP (One Time Password) using a simple Laravel package. https://github.com/seshac/otp-generator.

So, Let's start and follow below three simple steps.

  1. You can generate OTP using any unique identification (Prefer Mobile number or Email),
  2. Send to end user using any notification provider
  3. Finally, verify using the same unique identification.

Installation and Setup process :

Installation

You can install the package via composer:

composer require seshac/otp-generator
Enter fullscreen mode Exit fullscreen mode

You can publish and run the migrations with:

php artisan vendor:publish --provider="Seshac\Otp\OtpServiceProvider" --tag="migrations"
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

You can publish the config file with:

php artisan vendor:publish --provider="Seshac\Otp\OtpServiceProvider" --tag="config"
Enter fullscreen mode Exit fullscreen mode

Once you published you can configure all otp-generator.php config values based on your requirements.

Usage

use Seshac\Otp\Otp;
.
.
$otp =  Otp::generate($identifier);
.
$verify = Otp::validate($identifier, $otp->token);
// response
{
  "status": true
  "message": "OTP is valid"
}
Enter fullscreen mode Exit fullscreen mode

You have control to update the setting at otp-generator.php config file, but you control while generating also

Advance Usage

use Seshac\Otp\Otp;
.
.
$otp =  Otp::setValidity(30)  // otp validity time in mins
      ->setLength(4)  // Lenght of the generated otp
      ->setMaximumOtpsAllowed(10) // Number of times allowed to regenerate otps
      ->setOnlyDigits(false)  // generated otp contains mixed characters ex:ad2312
      ->setUseSameToken(true) // if you re-generate OTP, you will get same token
      ->generate($identifier);
.
$verify = Otp::setAllowedAttempts(10) // number of times they can allow to attempt with wrong token
    ->validate($identifier, $otp->token);

Enter fullscreen mode Exit fullscreen mode

Here the GitHub repository https://github.com/seshac/otp-generator

If you find any issues, please, feel to free open an issue here.
https://github.com/seshac/otp-generator/issues

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (7)

Collapse
 
samikos profile image
samik-os

Is there a way to forcibly expire tokens after they are verified?

Collapse
 
sesha profile image
Sesha

No, Please open a issue on github and we can work on that.

Collapse
 
masedi profile image
Edi Septriyanto

Is Laravel 8 supported?

Collapse
 
sesha profile image
Sesha

Yes, it's

Collapse
 
mdhesari profile image
Mohammad Fazel

Love it

Collapse
 
rubab2020 profile image
Imran Hossain (Rubab)

migration file is not generated

Collapse
 
sesha profile image
Sesha

Please create an issue with details, we will fix soon!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️