A Laravel package for generating, validating, and managing One-Time Passwords (OTP) with security features.
- ✅ Rate-limited OTP generation
- ✅ Configurable expiration times
- ✅ Invalidate OTP after first use
- ✅ Lock OTP to user session
- ✅ Invalidate OTP after too many failed attempts
- ✅ View detailed error messages
- ✅ Customizable mail template
- ✅ Auditable logs for security
composer require putheakhem/otp
php artisan vendor:publish --provider="PutheaKhem\Otp\Providers\OtpServiceProvider"
php artisan migrate
This will create:
- A config file at
config/otp.php
- A database table
otps
Modify config/otp.php
to adjust settings:
return [
'length' => 6, // OTP length
'expires_in' => 300, // OTP expiration time in seconds (5 minutes)
'max_attempts' => 5, // Maximum failed attempts before invalidation
'lock_to_session' => true, // OTP tied to user session
'mail_template' => 'otp::emails.otp', // Email template for OTP
'logging_enabled' => true, // Enable OTP logging
];
use PutheaKhem\Otp\Facades\Otp;
$otp = Otp::generate('[email protected]');
dd($otp);
📌 Output Example:
PutheaKhem\Otp\Models\Otp {#123
id: 1,
identifier: "[email protected]",
otp: "123456",
used: false,
attempts: 0,
expires_at: "2025-02-10 12:00:00"
}
$response = Otp::validate('[email protected]', '123456');
dd($response);
📌 Expected Output: ✅ Success
{
"status": true,
"message": "OTP verified successfully."
}
❌ Failure (Invalid OTP)
{
"status": false,
"message": "Invalid OTP."
}
❌ Failure (Expired OTP)
{
"status": false,
"message": "OTP expired or invalid."
}
Customize the email template at:
resources/views/vendor/otp/emails/otp.blade.php
Example:
<!DOCTYPE html>
<html>
<head>
<title>OTP Verification</title>
</head>
<body>
<p>Your OTP is: <strong>{{ $otp }}</strong></p>
<p>This OTP is valid for {{ config('otp.expires_in') / 60 }} minutes.</p>
</body>
</html>
php artisan test
📌 Expected Output:
✔ can generate OTP
✔ can validate OTP
✔ OTP invalid after expiry
✔ OTP fails after too many attempts
✔ OTP logs events
✔ Emails are sent correctly
✔ OTP is locked to session
- Fork the repository
- Clone the repo:
git clone https://github.com/putheakhem/otp.git
- Create a new branch:
git checkout -b feature-branch
- Commit changes & push:
git commit -m "Added new feature" git push origin feature-branch
- Submit a Pull Request 🚀
Developed by Puthea Khem.
Special thanks to the Laravel community! 🎉
This package is open-source and licensed under the MIT License.