Otpz

Otpz stats

Downloads
59
Stars
127
Open Issues
0
Forks
3

View on GitHub →

Secure One-Time Passwords For Laravel

OTPz Screenshot

First Factor One-Time Passwords for Laravel

This package provides secure first factor one-time passwords (OTPs) for Laravel applications. Users enter their email and receive a one-time code to sign in.

  • ✅ Rate-limited
  • ✅ Configurable expiration
  • ✅ Invalidated after first use
  • ✅ Locked to the user's session
  • ✅ Invalidated after too many failed attempts
  • ✅ Detailed error messages
  • ✅ Customizable mail template
  • ✅ Auditable logs

Installation

1. Install the package via composer:

composer require benbjurstrom/otpz

2. Publish and run the migrations

php artisan vendor:publish --tag="otpz-migrations"
php artisan migrate

3. Add the package's interface and trait to your Authenticatable model

// app/Models/User.php
namespace App\Models;
 
//...
use BenBjurstrom\Otpz\Models\Concerns\HasOtps;
use BenBjurstrom\Otpz\Models\Concerns\Otpable;
 
class User extends Authenticatable implements Otpable
{
use HasFactory, Notifiable, HasOtps;
 
// ...
}

4. Add the package provided routes

// routes/web.php
Route::otpRoutes();

5. (Optional) Publish the views for custom styling

php artisan vendor:publish --tag="otpz-views"

This package publishes the following views:

resources/
└── views/
└── vendor/
└── otpz/
├── otp.blade.php (for entering the OTP)
├── components/template.blade.php
└── mail/
├── notification.blade.php (standard template)
└── otpz.blade.php (custom template)

6. (Optional) Publish the config file

php artisan vendor:publish --tag="otpz-config"

This is the contents of the published config file:

<?php
 
return [
/*
|--------------------------------------------------------------------------
| Expiration and Throttling
|--------------------------------------------------------------------------
|
| These settings control the security aspects of the generated codes,
| including their expiration time and the throttling mechanism to prevent
| abuse.
|
*/
 
'expiration' => 5, // Minutes
 
'limits' => [
['limit' => 1, 'minutes' => 1],
['limit' => 3, 'minutes' => 5],
['limit' => 5, 'minutes' => 30],
],
 
/*
|--------------------------------------------------------------------------
| Model Configuration
|--------------------------------------------------------------------------
|
| This setting determines the model used by Otpz to store and retrieve
| one-time passwords. By default, it uses the 'App\Models\User' model.
|
*/
 
'models' => [
'authenticatable' => env('AUTH_MODEL', App\Models\User::class),
],
 
/*
|--------------------------------------------------------------------------
| Mailable Configuration
|--------------------------------------------------------------------------
|
| This setting determines the Mailable class used by Otpz to send emails.
| Change this to your own Mailable class if you want to customize the email
| sending behavior.
|
*/
 
'mailable' => BenBjurstrom\Otpz\Mail\OtpzMail::class,
 
/*
|--------------------------------------------------------------------------
| Template Configuration
|--------------------------------------------------------------------------
|
| This setting determines the email template used by Otpz to send emails.
| Switch to 'otpz::mail.notification' if you prefer to use the default
| Laravel notification template.
|
*/
 
'template' => 'otpz::mail.otpz',
// 'template' => 'otpz::mail.notification',
];

Usage

Laravel Breeze Livewire Example

  1. Replace the Breeze provided App\Livewire\Forms\LoginForm::authenticate method with a sendEmail method that runs the SendOtp action. Also be sure to remove password from the LoginForm's properties.
// app/Livewire/Forms/LoginForm.php
 
use BenBjurstrom\Otpz\Actions\SendOtp;
use BenBjurstrom\Otpz\Exceptions\OtpThrottleException;
use BenBjurstrom\Otpz\Models\Otp;
//...
 
#[Validate('required|string|email')]
public string $email = '';
 
#[Validate('boolean')]
public bool $remember = false;
//...
 
public function sendEmail(): Otp
{
$this->validate();
 
$this->ensureIsNotRateLimited();
RateLimiter::hit($this->throttleKey(), 300);
 
try {
$otp = (new SendOtp)->handle($this->email, $this->remember);
} catch (OtpThrottleException $e) {
throw ValidationException::withMessages([
'form.email' => $e->getMessage(),
]);
}
 
RateLimiter::clear($this->throttleKey());
 
return $otp;
}
  1. Update resources/views/livewire/pages/auth/login.blade.php such that the login function calls our new sendEmail method and redirects to the OTP entry page. You can also remove the password input field in this same file.
public function login(): void
{
$this->validate();
 
$otp = $this->form->sendEmail();
 
$this->redirect($otp->url);
}

Laravel Breeze Inertia Example

  1. Replace the Breeze provided App\Http\Requests\Auth\LoginRequest::authenticate method with a sendEmail method that runs the SendOtp action. Also be sure to remove password from the rules array.
// app/Http/Requests/Auth/LoginRequest.php
 
use BenBjurstrom\Otpz\Actions\SendOtp;
use BenBjurstrom\Otpz\Exceptions\OtpThrottleException;
use BenBjurstrom\Otpz\Models\Otp;
//...
 
public function rules(): array
{
return [
'email' => ['required', 'string', 'email']
];
}
//...
 
public function sendEmail(): Otp
{
$this->ensureIsNotRateLimited();
RateLimiter::hit($this->throttleKey(), 300);
 
try {
$otp = (new SendOtp)->handle($this->email, $this->remember);
} catch (OtpThrottleException $e) {
throw ValidationException::withMessages([
'email' => $e->getMessage(),
]);
}
 
RateLimiter::clear($this->throttleKey());
 
return $otp;
}
  1. Update the App\Http\Controllers\Auth\AuthenticatedSessionController::store method to call our new sendEmail method and redirect to the OTP entry page.
public function store(LoginRequest $request): \Symfony\Component\HttpFoundation\Response
{
$otp = $request->sendEmail();
 
return Inertia::location($otp->url);
}
  1. Remove the password input field from the resources/js/Pages/Auth/Login.vue file.

Everything else is handled by the package components.

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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Benbjurstrom Otpz Related Articles

First Factor One-Time Passwords for Laravel with OTPZ image

First Factor One-Time Passwords for Laravel with OTPZ

Read article
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises