Introduction
Password reset is an essential authentication flow for web applications. If you use a starter kit, this feature is built for you automatically, but for API-only projects and projects with a custom UI, you’ll need to implement it manually. When manual implementation is required:- API-only backends (frontend is an SPA or mobile app)
- Building your own authentication UI without using a starter kit
- When you want to fully customize the mail notification and reset URL design
If you created your project using a starter kit (
laravel new), password reset is already implemented. This page describes how to implement it without a starter kit.Configuration
Password reset configuration is managed under thepasswords key in config/auth.php.
driver— how data is stored (databaseorcache)expire— token expiration (in minutes). Default is 60 minutesthrottle— wait time (in seconds) before another send is allowed
Drivers
The database driver
The default driver. It stores password reset tokens in the databasepassword_reset_tokens table. This table is included in Laravel’s default migration (0001_01_01_000000_create_users_table.php).
The cache driver
Thecache driver stores tokens in the cache store. It removes the need for the password_reset_tokens migration. Tokens are stored keyed by the user’s email address, so avoid using the email address as a cache key elsewhere in your app.
store key prevents reset data from being erased by php artisan cache:clear. The value should match a store name configured in config/cache.php.
Preparing your model
To use password reset, theApp\Models\User model needs two traits.
Notifiable— required to send mail notificationsCanResetPassword— provides methods needed for generating and verifying password reset tokens
Laravel’s default
User model already includes these traits. On a fresh install you don’t need to add them.Implementing routes
Password reset requires four routes.1. The reset link request form
Displays a form for the user to enter their email address.2. Handling the reset link send
Receives the form submission and usesPassword::sendResetLink() to send the reset email.
Password::sendResetLink() returns a status string.
3. The password reset form
Displays a form for the user to enter a new password after clicking the link in the email.4. Executing the password reset
Receives the form and updates the password withPassword::reset().
Password::reset():
Token expiration
You can set the token expiration in minutes via theexpire option in config/auth.php. The default is 60 minutes.
database driver, expired tokens remain in the database. To clean them up periodically, use the following Artisan command.
Customization
Using a custom notification
To customize the password reset email, override thesendPasswordResetNotification method on your User model.
Customizing the reset link URL
UseResetPassword::createUrlUsing() in your AppServiceProvider’s boot method to change the reset link URL. This is useful when redirecting to a frontend on a different origin, such as an SPA.
Configuring Trusted Hosts
Password reset links are generated using theHost header of the HTTP request. To prevent requests from unauthorized hosts, we recommend configuring Trusted Hosts in bootstrap/app.php.
Summary
Next steps
Authentication introduction
Learn the overall architecture of Laravel’s authentication system.
Notifications
Learn about customizing mail notifications in detail.