You may use the Composer package manager to install Otp Generator into your Laravel project:
composer require danielrobert/otp-generator
After installing the otp-generator, publish its configs using the otp:install Artisan command. After installing Otp Generator, you should also run the migrate command in order to create the tables needed to store OTPs:
php artisan otp:install
php artisan migrate
After publishing Otp Generator's configs, its primary configuration file will be located at config/otp-generator.php. This configuration file allows you to configure your otps. Each configuration option includes a description of its purpose, so be sure to thoroughly explore this file.
Data Pruning Without pruning, the otps table can accumulate records very quickly. To mitigate this, you can schedule the otp:prune Artisan command to run daily:
$schedule->command('otp:prune')->daily();
By default, all expired entries or entries older than 30 minutes as configured in the config/otp-generator.php will be pruned. You may use the minutes option when calling the command to determine how long to retain data. For example, the following command will delete all records created over 60 minutes ago:
$schedule->command('otp:prune --minutes=60')->daily();
use DanielRobert\Otp\Otp;
.
.
$otp = Otp::generate($identifier);
.
$verify = Otp::validate($identifier, $otp->token);
// example response
{
"status": true
"message": "Otp is valid"
}
// to get an expiredAt time
$expires = Otp::expiredAt($identifier);
// example response
+"status": true
+"expired_at": Illuminate\Support\Carbon @1611895244^ {
....
#dumpLocale: null
date: 2021-01-29 04:40:44.0 UTC (+00:00)
}
In addition to configuring otps from the otp-generator.php config file you can also configure it directly
use DanielRobert\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);
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.