How to implement password reset function in Laravel


brief introduction

Want to quickly implement this function? Only need to run under the newly installed Larravel application php artisan make:auth (If you have executed this command, you can ignore it), and then access http://your-app.dev/register Or other URLs assigned to the application. This command will generate everything the user needs to log in and register, including password reset!

Most web applications provide the function of resetting passwords for users, and Larvel is no exception. Larvel provides a convenient method for sending password reset links and implementing password reset logic, without requiring you to repeat it in each application.

Note: Before using the password reset function provided by Larave, User The model must use Illuminate\Notifications\Notifiable trait。

Database dependency

Verify before starting App\User The model implements Illuminate\Contracts\Auth\CanResetPassword Covenants. Of course, it comes with Larravel App\User The model has implemented the interface and used Illuminate\Auth\Passwords\CanResetPassword Trait to include the methods needed to implement the interface.

Generate Reset Token Table Migration

Next, the table used to store the password reset token must be created. Larvel has already brought its own migration of this table, which is stored in database/migrations catalog. So all you have to do is run the migration:

 php artisan migrate

This table is password_resets

route

Larave brought it Auth\ForgotPasswordController and Auth\ResetPasswordController Controller (these two controller classes will pass the php artisan make:auth Command is automatically generated), which is used to send password reset link email and reset user password respectively. All routes required for password reset have been passed make:auth The command was automatically generated:

 php artisan make:auth

The corresponding route is defined in Illuminate\Routing\Router Of auth In the method:

view

As with routing, the view file required to reset the password is also passed make:auth The commands are generated together. These view files are located in resources/views/auth/passwords Directory, you can modify the generated files as required.

reset password

After the user password reset route and view are defined, you only need to use the /password/reset Access this entry route. Built in frame ForgotPasswordController The logic of sending password reset link email has been included, ResetPasswordController It contains the logic to reset the user password:

Enter the registered email address, click the Send Password Reset link, and the password reset link will be sent to the email address:

When you open the mailbox, you will receive an email of password reset:

Click the Reset Password button to enter the Reset Password page:

You can reset your password after filling out the form and submitting it.

After the password is reset, the user will automatically log in to the application and redirect to /home You can define ResetPasswordController Of redirectTo Property to customize the jump link after successful password reset:

 protected $redirectTo = '/dashboard';
Note: By default, the password reset token is valid within one hour. You can modify it by config/auth.php Options in file expire To change the effective time.

custom

Custom Authentication Guard

In the configuration file auth.php Multiple "guards" can be configured to achieve independent authentication based on multi-user tables. You can override the built-in ResetPasswordController On the controller guard Method to use the selected guard. This method will return a guard instance:

 use Illuminate\Support\Facades\Auth; protected function guard() { return Auth::guard('guard-name'); }

Custom password broker

In the configuration file auth.php You can configure multiple passwords to reset the password broker of multiple user tables. Similarly, you can override the built-in ForgotPasswordController and ResetPasswordController In the controller broker Method to use the broker you selected:

 use Illuminate\Support\Facades\Password; /** *Get the broker used during password reset * * @return PasswordBroker * @translator laravelacademy.org */ protected function broker() { return Password::broker('name'); }

Custom Password Reset Message

You can easily edit the notification class that sends the password reset link to users to implement custom password reset emails. To achieve this function, you need to rewrite User On model sendPasswordResetNotification Method. In this method, you can use any notification class you like to send notifications. The first parameter received by this method is password reset $token

 /** *Send password reset notification * * @param  string  $token * @return void */ public function sendPasswordResetNotification($token) { $this->notify(new ResetPasswordNotification($token)); }

give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: Using Hash Algorithm to Implement Password Encryption in Laravel

>>Next: Quick start: basic configuration and use, read-write separation&database transaction