首页 > 解决方案 > ResetPasswordController 如何使用密码重置表 Laravel

问题描述

内置密码重置控制器 Auth\Reset Password Controller 具有重置功能

public function reset(Request $request)
{
$request->validate($this->rules(), $this->validationErrorMessages());

// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
// database. Otherwise we will parse the error and return the response.
$response = $this->broker()->reset(
    $this->credentials($request), function ($user, $password) {
        $this->resetPassword($user, $password);
    }
);

// If the password was successfully reset, we will redirect the user back to
// the application's home authenticated view. If there is an error we can
// redirect them back to where they came from with their error message.
return $response == Password::PASSWORD_RESET
            ? $this->sendResetResponse($request, $response)
            : $this->sendResetFailedResponse($request, $response);
}

好吧,这里我们正在处理用户及其传入的数据。但是,我不明白 password_resets 表(内置)的工作在哪里?毕竟,在密码恢复后,条目会在那里添加/删除。我认为(可能不正确)这是在 broker() 方法中实现的,但我在特征、接口和其他类的层次结构中找不到它。

标签: phplaravel

解决方案


结帐/vendor/laravel/framework/src/Illuminate/Auth/Passwords/DatabaseTokenRepository.php

这是实现 的默认类,TokenRepositoryInterface并由/vendor/laravel/framework/src/Illuminate/Auth/Passwords/PasswordBroker.php.

在此类中,您可以找到处理密码重置的所有实际功能,包括您提到的表操作。例如,您会发现一种方法是:

/**
 * Create a new token record.
 *
 * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
 * @return string
 */
public function create(CanResetPasswordContract $user)
{
    $email = $user->getEmailForPasswordReset();

    $this->deleteExisting($user);

    // We will create a new, random token for the user so that we can e-mail them
    // a safe link to the password reset form. Then we will insert a record in
    // the database so that we can verify the token within the actual reset.
    $token = $this->createNewToken();

    $this->getTable()->insert($this->getPayload($email, $token));

    return $token;
}

推荐阅读