turahe / otp
send OTP to every where
Requires
- php: ^8.0
- giggsey/libphonenumber-for-php: 8.0
- illuminate/container: ^8.0|^9.0|^10.0
- illuminate/notifications: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.1
- orchestra/testbench: ^6.23|^7.0
This package is auto-updated.
Last update: 2024-12-08 02:41:15 UTC
README
Introduction ๐
This is a simple package to generate and validate OTPs (One Time Passwords). This can be implemented mostly in Authentication.
Installation ๐ฝ
Install via composer
composer require turahe/otp
Add service provider to the config/app.php
file
<?php /* |-------------------------------------------------------------------------- | Autoloaded Service Providers |-------------------------------------------------------------------------- | | The service providers listed here will be automatically loaded on the | request to your application. Feel free to add your own services to | this array to grant expanded functionality to your applications. | */ 'providers' => [ ... Turahe\Otp\OtpServiceProvider::class, ]; ...
Add alias to the config/app.php
file
<?php /* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don't hinder performance. | */ 'aliases' => [ ... 'Token' => Turahe\Otp\Otp::class, ]; ...
Run Migrations
php artisan migrate
Usage ๐งจ
NOTE
Response are returned as objects. You can access its attributes with the arrow operator (->
)
Generate OTP
<?php Otp::generate(string $identifier int $validity = 10)
$identifier
: The identity that will be tied to the OTP.$validity (optional | default = 10)
: The validity period of the OTP in minutes.
Sample
<?php use Turahe\otp\Facades\Otp; $otp = Otp::generate('nur@wach.id', 15);
This will generate a six digit OTP that will be valid for 15 minutes and the success response will be:
{
"id": 1,
"identity": "nur@wach.id",
"token": "282581",
"created_at": "2020-01-01 00:00:00",
"updated_at": "2020-01-01 00:00:00"
}
Validate OTP
<?php Otp::validate(string $identifier, string $token)
$identifier
: The identity that is tied to the OTP.$token
: The token tied to the identity.
Sample
<?php $otp = Otp::validate('nur@wach.id', '282581');
Delete expired tokens
You can delete expired tokens by running the following artisan command:
php artisan otp:clean
You can also add this artisan command to app/Console/Kernel.php
to automatically clean on scheduled
<?php protected function schedule(Schedule $schedule) { $schedule->command('otp:clean')->daily(); }
Contribution
If you find an issue with this package or you have any suggestion please help out. I am not perfect.