reset Password


Routing

in the new Laravel application, and then open

http://your-app.test/register in the browser or give it to you The application is assigned an arbitrary URL. This command will be responsible for setting up the entire authentication system, including resetting the password!

Most web applications provide users with the ability to reset their passwords. Rather than forcing you to re-implement this functionality in every application, Laravel provides convenient ways to send password reminders and perform password resets.

{note} Before using Laravel's password reset functionality, your user model must use the
Illuminate\Notifications\Notifiable

trait.

Database Notes

First, verify that your App\User model implements the Illuminate\Contracts\Auth\CanResetPassword contract. Of course, the App\User model included in the framework already implements this interface, and uses the Illuminate\Auth\Passwords\CanResetPassword trait t to include the methods required to implement the interface.

Table migration to generate reset token

Next, a data table must be created to store the password reset token. Migrations for this data table are included in the database/migrations directory of your Laravel application. So, all you need to do is execute the database migration command:

php artisan migrate

##routing

Laravel is already in

The Auth\ForgotPasswordController and Auth\ResetPasswordController classes contain the logic for sending the password reset link email and resetting the user's password. All routes required to perform a password reset can be generated using the Artisan command make:auth:

php artisan make:auth

Views

When you execute the

make:auth command, Laravel generates all the views needed to reset your password. These views are located in the resources/views/auth/passwords directory. You are free to modify these views according to the needs of your application.

After resetting the password

Once you have defined the route and view for resetting the user password, you can Access the

/password/reset route in your browser to reset your password. ForgotPasswordController in the framework already contains the logic to send the password reset link email, and ResetPasswordController contains the logic to reset the user password.

After resetting the password, the user will automatically log in and be redirected to

/home. You can customize the location of redirection after password reset by defining a redirectTo property in ResetPasswordController:

protected $redirectTo = '/dashboard';

{note} By default, The password reset token expires after one hour. You can modify this via the password reset

expire option in the config/auth.php file.

Custom

Custom Authentication Watcher

In your

auth.php configuration file, you can configure multiple "watchers", which can be used to define the authentication behavior of multiple user tables. You can customize ResetPasswordController in the framework and use the guard of your choice by overriding the guard method in the controller. This method should return a guard instance:

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

Custom Password Proxy

In your auth.php configuration file, you can configure multiple password "proxies" that can be used to reset the password on multiple user tables. password. You can customize ForgotPasswordController and ResetPasswordController in the framework by overriding the broker method in the controller to use the broker of your choice:

use Illuminate\Support\Facades\Password;
/**
 * 获取在密码重置期间使用的代理。
 *
 * @return PasswordBroker
 */
 protected function broker(){  
   return Password::broker('name');
 }

Custom Password Reset Email

You can easily modify the notification class used to send password reset links to users. First, override the sendPasswordResetNotification method in the User model. In this method, you can use any notification class of your choice to send the notification. The first parameter received by this method is the password reset token $token:

/**
 * 发送密码重置通知。
 *
 * @param  string  $token
 * @return void
 */
 public function sendPasswordResetNotification($token){ 
    $this->notify(new ResetPasswordNotification($token));
 }
This article was first published on the LearnKu.com website.