代码之家  ›  专栏  ›  技术社区  ›  Javed

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

  •  1
  • Javed  · 技术社区  · 7 年前

    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 未在Live中更新文件夹。解决方案是什么?如何覆盖此验证?

    这里是我的 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 回复  |  直到 7 年前
        1
  •  4
  •   thefallen    7 年前

    以下是解决此问题的方法:

    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 .