morcen / laravel-otp-generator
Generate OTP for your Laravel application
Fund package maintenance!
morcen
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: 2024-11-10 01:24:09 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.php
and 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 froma
toz
uppercase
- English alphabet fromA
toZ
others
- 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 inothers
option. This is the defualt option.
lifetime
- defines how long the OTP will be valid for. Default is15
minutes.length
- defines the default length of the OTP. Though this can be simply overriden whenever thegenerate
orgenerateFor
methods are called. Default length is4
.encrypt
- if set totrue
, it will have additional propertyhash
in 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 ifencrypt
is set totrue
. Valid options aresha1
andmd5
. Available sincev1.1.0
.
-
Publish the migrations with:
php artisan vendor:publish --tag="otp-generator-migrations"
and update it to include the
identifier
field 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 defaultlength
option 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.