Created
December 2, 2021 13:46
-
-
Save EduardoSP6/b8aff237f7d9dbb22e0a8985e825811a to your computer and use it in GitHub Desktop.
Password reset laravel 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Password reset laravel 7: | |
- Add the traits below in model User: | |
Illuminate\Notifications\Notifiable; | |
Illuminate\Contracts\Auth\CanResetPassword; | |
- Install laravel/ui that contains the migration of reset token table. Commands: | |
composer require laravel/ui | |
php artisan migrate | |
- Controllers: Auth\ForgotPasswordController and Auth\ResetPasswordController; | |
- Path of views for customization: resources/views/auth/passwords/ | |
- After reset password: | |
After define the routes and views to reset the users password, you can access the route /password/reset in your browser. | |
The ForgotPasswordController included in Laravel already has the logic for send emails of password reset, | |
and the ResetPasswordController has methods to redirect user before reset and password rules that you can modify | |
whenever you want. | |
After a password is reset, the user will automatically be logged in to the app and redirect for /home route. | |
You can change it modifying redirectTo property of ResetPasswordController. | |
By default, reset tokens expire after 60 seconds. | |
You can change this in Resetting Passwords section of config/auth.php. | |
- Customizing the notification: | |
You can easily the notification class used to send a link for password reset. | |
For that, override the sendPasswordResetNotification method in User model and put your logic inside. | |
You can create a Mail class for that. | |
public function sendPasswordResetNotification($token) | |
{ | |
Mail::to($request->user())->send(new ResetPassword($token, $this->getEmailForPasswordReset())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment