jefte / otpgenerator
Laravel plugin to generate OTP Tokens
0.0.2
2021-09-15 04:39 UTC
Requires
- php: ^7.3
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-03-16 05:09:48 UTC
README
Requirements
- Laravel v6.0 or above
- Composer
Installation
Via Composer, run:
composer require jefte/otpgenerator
Publish migration for OTP Tokens:
php artisan vendor:publish --provider=Jefte\OTPGenerator\OTPServiceProvider --tag=otp-migration
then run php artisan migrate
Usage
Include the HasOTPToken
trait into your model
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Jefte\OTPGenerator\Traits\HasOTPToken; class User extends Authenticatable { use HasFactory, Notifiable, HasOTPToken; }
To Generate an OTP Token, use the createOtpToken
method from the model
class HomeController extends Controller { public function requestOtpToken(Request $request) { $user = $request->user(); // Creating Token $token = $user->createOtpToken(); // Accessing the created token $token->getGeneratedToken(); // Sample Use Case $user->notify(new SendOtpNotification($token)); } public function verifyOtpToken(Request $request) { $user = $request->user(); // To Verify Token $token = $request->input('otp_token'); if(! $user->verifyToken($myToken)) { return response()->json([ 'message' => 'Invalid OTP' ], 422); } } }
Customization
Publish config
php artisan vendor:publish --provider=Jefte\OTPGenerator\OTPServiceProvider --tag=otp-config
In your .env file
# Default Characters generated OTP Token OTP_CHARACTERS=123456789 # Default Length OTP_LENGTH=6 # Token default TTL # 3600 secs = 1Hour # Use seconds as unit OTP_TTL=3600