morcen / laravel-otp-generator
Generate OTP for your Laravel application
Fund package maintenance!
morcen
Installs: 93
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 7
pkg:composer/morcen/laravel-otp-generator
Requires
- php: ^8.0
- laravel/framework: ^8.0 | ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- pestphp/pest-plugin-mock: ^1.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2025-10-10 03:20:05 UTC
README
Generate OTP for your Laravel application
This package requires Laravel >= 8.x.
Installation
-
Install the package via composer:
composer require morcen/laravel-otp-generator
-
Publish the config file with:
php artisan vendor:publish --tag="otp-generator-config"Open
config/otp.phpand update the following:identifier- this is what this package will use to query for the OTP record in the databaseset- this defines the characters that will be used for the code. Possible values are:numbers- from zero to nine (0 to 9)lowercase- English alphabet fromatozuppercase- English alphabet fromAtoZothers- defaults to empty string. You can put any other characters you wish to see as part of the OTP code.all- uses all letters and numbers, including the characters inothersoption. This is the defualt option.
lifetime- defines how long the OTP will be valid for. Default is15minutes.length- defines the default length of the OTP. Though this can be simply overriden whenever thegenerateorgenerateFormethods are called. Default length is4.encrypt- if set totrue, it will have additional propertyhashin the returned object. If used withgenerateFor()method, the value that will be saved in the database will be the encrypted value. Available sincev1.1.0.alg- This will be the hashing algorithm used for the OTP code. This will only take effect ifencryptis set totrue. Valid options aresha1andmd5. Available sincev1.1.0.
-
Publish the migrations with:
php artisan vendor:publish --tag="otp-generator-migrations"and update it to include the
identifierfield mentioned in the previous step. For example, if you have'identifier' => 'email'
in your
config/otp.php, you have to add this line in the migration file created:Schema::create('otps', function (Blueprint $table) { $table->id(); $table->string('email'); // <-- this is the line added to match the `identifier` $table->string('code'); $table->unsignedInteger('expiration'); $table->timestamp('created_at'); });
-
Run the migrations with:
php artisan migrate
Usage
To generate an OTP that can be validated afterwards, use the method generatedFor(). It accepts two parameters:
$identifierValue(required) - the value that will be matched against theidentifier$length(optional) - the length of the code to use. If this is not provided, it will use the defaultlengthoption set inconfig/otp.php.
For example, we want to create an OTP for the email abcd@example.com:
use Morcen\LaravelOtpGenerator\Facades\Otp; $otp = Otp::generateFor('abcd@example.com');
$otp receives back the OTP object*, e.g.
{
"email": "abcd@example.com",
"code": "028988",
"hash": "6120a26f84406f452c1b27509505093ba4aa263d",
"expiration": 1647363156
}
And then to validate, use the method validateFor, accepting the $identifierValue and the OTP as parameters and returns bool:
Otp::validateFor('abcd@example.com', '028988'); // returns `true`
*NOTE: hash property only appears in the OTP object if encrypt is set to true.
To generate and receive an OTP code only:
use Morcen\LaravelOtpGenerator\Facades\Otp; $otp = Otp::generate();
$otp receives back just the OTP object like this:
{
"code": "234198",
"hash": "858edae3093a5b6f5b7812cff2e2e369da0a2290"
}
*NOTE: hash property only appears in the OTP object if encrypt is set to true.
To override the default length of the OTP, pass the length as a parameter. For example, to generate an OTP that is 10 characters long:
$otp = Otp::generateFor('abcd@example.com', 10);
or
$otp = Otp::generate(10);
Testing
composer test
Changelog
Please see CHANGELOG 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.