Laravel : 重置密码得到 6 位数字,无需验证
Laravel : Reset password get 6 digits without validation
我有重置密码的简单功能。在我的函数中,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)
]);
}
这是解决此问题的方法:
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
。在那里你只需要 return true 因为 PasswordBroker 已经有了支票 $password === $confirm
.
我有重置密码的简单功能。在我的函数中,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)
]);
}
这是解决此问题的方法:
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
。在那里你只需要 return true 因为 PasswordBroker 已经有了支票 $password === $confirm
.