You can install the package via composer:
composer require spatie/laravel-one-time-passwords
##Migrating the database
This package can store one-time passwords in the database. You can create the one_time_passwords
table by publishing and running the migrations.
php artisan vendor:publish --tag="one-time-passwords-migrations"
php artisan migrate
##Preparing your model
You should let your User
model use the HasOneTimePasswords
trait.
namespace App\Models;
use Spatie\LaravelOneTimePasswords\Models\Concerns\HasOneTimePasswords;
class User
{
use HasOneTimePasswords;
}
##Deleting expired one-time passwords
This package uses the MassPrunable
trait provided by Laravel.
To delete expired one-time password, you can add the model:prune
command to your schedule.
Here's an example where expired one-time passwords are deleted daily.
use Spatie\LaravelOneTimePasswords\Models\OneTimePassword;
Schedule::command('model:prune', [
'--model' => [OneTimePassword::class],
])->daily()
##Publishing the config file
Optionally, you can publish the one-time-passwords
config file with this command.
php artisan vendor:publish --tag="one-time-password-config"
This is the content of the published config file:
return [
'default_expires_in_minutes' => 2,
'only_one_active_one_time_password_per_user' => true,
'enforce_same_origin' => true,
'origin_enforcer' => Spatie\LaravelOneTimePasswords\Support\OriginInspector\DefaultOriginEnforcer::class,
'password_generator' => Spatie\LaravelOneTimePasswords\Support\PasswordGenerators\NumericOneTimePasswordGenerator::class,
'password_length' => 6,
'redirect_successful_authentication_to' => '/dashboard',
'rate_limit_attempts' => [
'max_attempts_per_user' => 5,
'time_window_in_seconds' => 60,
],
'model' => Spatie\LaravelOneTimePasswords\Models\OneTimePassword::class,
'notification' => Spatie\LaravelOneTimePasswords\Notifications\OneTimePasswordNotification::class,
'actions' => [
'create_one_time_password' => Spatie\LaravelOneTimePasswords\Actions\CreateOneTimePasswordAction::class,
'consume_one_time_password' => Spatie\LaravelOneTimePasswords\Actions\ConsumeOneTimePasswordAction::class,
],
];