Coder Social home page Coder Social logo

laravel-otp's Introduction

A composer package for generating and verifying One Time Passwords (OTP) in Laravel 11+.

Latest Version on Packagist run-tests Fix PHP code style issues Total Downloads

A composer package for generating and verifying One Time Passwords (OTP) in Laravel 11+.

Installation

You can install the package via composer:

composer require njoguamos/laravel-otp

You can publish and run the migrations with:

php artisan vendor:publish --tag="otp-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="otp-config"
This is the contents of the published config file:
return [

    /*
    |--------------------------------------------------------------------------
    |  OTP Length
    |--------------------------------------------------------------------------
    |
    | This is the length of the generated OTP token. By default it is set to
    | 6 digits. The length of the OTP must be at least 4 digits to ensure
    | that the OTP is not easily guessable
    |
    */

    'length' => env(key: 'OPT_LENGTH', default: 6),

    /*
    |--------------------------------------------------------------------------
    | OTP Validity time by minutes
    |--------------------------------------------------------------------------
    |
    | This is the validity time of the generated OTP token. By default it is
    | set to 10 minutes. This means that the OTP will be valid for 10 minutes
    | after it is generated. You can change this value to suit your needs.
    |
    */

    'validity' => env(key: 'OTP_VALIDITY', default: 10),

    /*
    |--------------------------------------------------------------------------
    | Digits Only
    |-------------------------------------------------------------------------
    |
    | When set to true, the generated OTP will only contain digits. When set
    | to false, the generated OTP will contain both digits and alphanumeric
    | characters which makes it more difficult to guess the OTP.
    |
    */

    'digits_only' => env(key: 'OTP_DIGITS_ONLY', default: true),
];

Usage

Generate OTP

To generate an OTP, you can use the generate() method on the Otp class. . This method takes an identifier as a parameter. The identifier can be and email address, phone number, or any other unique identifier that you want to use to identify the user.

use NjoguAmos\Otp\Otp;

$otp = Otp::generate(identifier: '[email protected]');

$otp->identifier; # [email protected]
$otp->token; # 123456
$otp->expires_in; # 10 minutes

The generate() method returns an instance of the \NjoguAmos\Otp\Models\Otp Eloquent Models class. You can access the identifier, token, and expires_at properties of the Otp class.

For example: you can use the token property to send the OTP to the user's email address.

use NjoguAmos\Otp\Otp;
use App\Mail\OTPMail;
use Illuminate\Support\Facades\Mail;

$email = '[email protected]';

$otp = Otp::generate(identifier: $email);

Mail::to($email)->send(new OTPMail($order));

Verify OTP

To verify an OTP, you can use the validate() method on the Otp class. This method takes an identifier and token as parameters.

If the OTP is valid, the method will return true. Otherwise, it will return false.

use NjoguAmos\Otp\Otp;

$email = '[email protected]';
$otp = '123456';

$validated = Otp::validate(identifier: $email, token: $otp->token);

$validated // True or False

Note

It is advisable not to let the user know if the OTP does not match, or does not exist or expired. You can return a generic message to the user instead.

Delete Expired OTPs

To periodically delete expired OTPs, you can use the model:prune Artisan command. This command will delete all expired OTPs from the database.

To do so, add model:prune to your routes/console.php file:

use Illuminate\Support\Facades\Schedule;
use NjoguAmos\Otp\Models\Otp as OtpModel;

Schedule::command('model:prune', ['--model' => [OtpModel::class]])->everyFiveMinutes();

Tip

Make sure the duration is greater than the validity time of the OTP.

Security

Tip

To prevent brute force attacks, rate limit the number of attempts to generate or verify an OTP tokens. This can be done by using the Laravel RateLimit middleware. User can verify tokens as long as they are valid. This means that the user can have multiple valid tokens. Once the token expires, it cannot be valid even if it still exists in the database. Schedule the model:prune command to run every 5 minutes to delete expired tokens.

Testing

composer test

Changelog

Please see RELEASE for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-otp's People

Contributors

njoguamos avatar dependabot[bot] avatar

Stargazers

Maman` avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.