danielrobert / otp-generator
A Laravel Package for Generating and Validating Otp using database
Requires
- php: ^8.0
- laravel/framework: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0|^9.0
- php-parallel-lint/php-parallel-lint: dev-develop
- phpunit/phpunit: ^9.0|^10.0|^11.0
- vimeo/psalm: ^5.8
README
Installation
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, you need to publish its configuration and migration files. The migration files will be copied to your application's database/migrations directory, but they are not run automatically. You must run the migrate command yourself to create the necessary tables:
php artisan vendor:publish --provider="DanielRobert\Otp\OtpGeneratorServiceProvider" --tag=otp-migrations php artisan vendor:publish --provider="DanielRobert\Otp\OtpGeneratorServiceProvider" --tag=otp-config php artisan migrate
Configuration
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
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();
Usage
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) }
Advance Usage
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);
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.