How to implement the Laravel password reset function

Source: Internet
Author: User
Want to quickly implement this feature? Just run PHP artisan Make:auth under the newly installed Laravel app (if you've already executed this command, you can ignore it), and then access Http:// or other URLs assigned to the app in your browser. This command generates everything a user needs to sign in to register, including password reset!

Most Web apps provide the ability to reset passwords for users, and Laravel is no exception, Laravel provides a convenient way to send password reset links and implement password reset logic without having to repeat the implementation yourself in each app.

Note: The User model must use the illuminate\notifications\notifiable trait before using the password reset feature provided by Laravel.

Database related

Before you begin, verify that the App\user model implements the Illuminate\contracts\auth\canresetpassword contract. Of course, Laravel's own App\user model has implemented the interface and uses Illuminate\auth\passwords\canresetpassword trait to contain the methods needed to implement the interface.

Generate a Reset token table migration

Next, the table used to store the password reset token must be created, and Laravel has already migrated the table, which is stored in the Database/migrations directory. So all you have to do is run the migration:

PHP Artisan Migrate

This table is password_resets:


The Laravel comes with the Auth\forgotpasswordcontroller and Auth\resetpasswordcontroller controllers (these two controller classes are automatically generated via the PHP Artisan make:auth command). Used to send password reset linked messages and reset user password functions, respectively. The route required to reset the password has been automatically generated by the Make:auth command:

PHP Artisan Make:auth

The corresponding route definition is in the Auth method of Illuminate\routing\router:


As with routing, the view files required to reset the password are also generated by the Make:auth command, which is located in the

In the Resources/views/auth/passwords directory, you can modify the generated files as needed.

Reset Password

Once you have defined the Reset user password route and view, you only need to access the portal route through/password/reset in the browser. The Forgotpasswordcontroller that comes with the framework already contains the logic to send the password reset link message, Resetpasswordcontroller contains the logic to reset the user's password:

Enter the registration email and click the Send password reset link to send a password reset link to the mailbox:

Opening a mailbox will receive a password reset message:

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

After completing the form submission, you can reset your password.

After the password is reset, the user will automatically log on to the app and redirect to/home. You can customize the jump link after a successful password reset by defining the Redirectto property of Resetpasswordcontroller:

protected $redirectTo = '/dashboard ';

Note: By default, the password reset token is valid for an hour, and you can change the effective time by modifying the option expire in the config/auth.php file.


Custom Authentication Guard

In profile auth.php, you can configure multiple "guards" to implement standalone authentication based on multi-user tables, and you can use the Guard method on the built-in Resetpasswordcontroller controller to The method will return a guard instance:

Use illuminate\support\facades\auth;protected function Guard () {return Auth::guard (' Guard-name ');}

Custom Password Broker

In profile auth.php, you can configure multiple passwords so that you can use password Broker to reset multiple user tables, as well, by overriding the Forgotpasswordcontroller in your own and resetpasswordcontroller controllers. Broker method to use the broker of your choice:

Use Illuminate\support\facades\password;

/** * Gets the broker used during the password reset. * * @return Passwordbroker * @translator */protected function broker () {  return Password::broker (' Name ');}

Custom password Reset messages

You can easily edit the Send password reset link to the user's notification class to implement a custom password reset message, to implement this function, you need to rewrite the user model on the Sendpasswordresetnotification method, in this method, You can use any of the notification classes you like to send notifications, the first parameter that the method receives is the password reset $token:

/** * Send password reset notifications. * * @param string $token * @return void */public function Sendpasswordresetnotification ($token) {  $this->notify ( New Resetpasswordnotification ($token));}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.