Laravel:重置密码获得6位数而无需验证

时间:2018-05-29 10:40:08

标签: laravel laravel-5.5

我有简单的功能来重置我的密码。在我的函数中,password值的最低要求是1 digit,但是当我尝试更新密码时,它不会更新,当我将6 digits放入密码时它工作正常。

我发现在vendor\laravel\framework\src\Illuminate\Auth\Passwords passwordBroker.php文件中有一个函数

 protected function validatePasswordWithDefaults(array $credentials)
{
    list($password, $confirm) = [
        $credentials['password'],
        $credentials['password_confirmation'],
    ];

    return $password === $confirm && mb_strlen($password) >= 6; // here it is
}

它包含验证($password) >= 6如何删除它,当我更改此文件时它正在工作。在我的.gitignore vendor文件夹中未在实时更新。解决办法是什么 ?如何覆盖此验证?

这里的参考是我的resetpassword函数

public function resetPassword(ResetPasswordRequest $request, JWTAuth $JWTAuth)
{
    $validator = Validator::make($request->all(), User::resetPasswordRules());
    if ($validator->fails()) {
        return response()->json([
            'message'       => "422 Unprocessable Entity",
            'errors'        => $validator->messages(),
            'status_code'   => 422,
        ]);
    }


    $response = $this->broker()->reset(
        $this->credentials($request), function ($user, $password) {
            $this->reset($user, $password);
        }
    );

    if($response !== Password::PASSWORD_RESET) {
        return response()->json([
                'message'       => "Internal Server Error",
                'status_code'   => 500,
            ]);
    }
    $user = User::where('email', '=', $request->get('email'))->first();
    $user->UserDeviceData()->firstOrCreate([
        'device_id' => $request->device_id
    ]);

     return (new UserTransformer)->transform($user,[
        'request_type'  => 'reset_password',
        'token'         =>  $JWTAuth->fromUser($user)
    ]);
}

1 个答案:

答案 0 :(得分:3)

这是解决这个问题的方法:

public function resetPassword(ResetPasswordRequest $request, JWTAuth $JWTAuth)
{
    ... // Validator check and json response

    $broker = $this->broker();

    // Replace default validation of the PasswordBroker
    $broker->validator(function (array $credentials) {
        return true; // Password match is already validated in PasswordBroker so just return true here
    });

    $response = $broker->reset(
        $this->credentials($request), function ($user, $password) {
        $this->reset($user, $password);
    });

    ...
}

首先生成代理的实例,然后添加一个可调用的函数,它将用于验证而不是validatePasswordWithDefaults。在那里你只需要返回true,因为PasswordBroker已经有一个支票$password === $confirm