在 Laravel 5.4 中更改密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42591795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Password In Laravel 5.4
提问by Fred Lopes
I've add code to change password in my webapp. But Hash::check()
doesn't work. Always return false. Also Hash::Make()
returns different string every time. I've tried bcrypt()
instead but, it also doesn't work. Please help me. Here's my code.
我在我的 webapp 中添加了代码来更改密码。但Hash::check()
不起作用。总是返回假。Hash::Make()
每次也返回不同的字符串。我已经尝试过了,bcrypt()
但它也不起作用。请帮我。这是我的代码。
public function changePassword(Request $request)
{
$user = Auth::user();
$curPassword = $request->input['curPassword'];
$newPassword = $request->input['newPassword'];
if (Hash::check($curPassword, $user->password)) {
$user_id = $user->id;
$obj_user = User::find($user_id)->first();
$obj_user->password = Hash::make($newPassword);
$obj_user->save();
return response()->json(["result"=>true]);
}
else
{
return response()->json(["result"=>false]);
}
}
Thank you.
谢谢你。
回答by kjdion84
Heres how I did it:
这是我如何做到的:
In my controller, I use the following method:
在我的控制器中,我使用以下方法:
public function changePassword()
{
$this->validate(request(), [
'current_password' => 'required|current_password',
'new_password' => 'required|string|min:6|confirmed',
]);
request()->user()->fill([
'password' => Hash::make(request()->input('new_password'))
])->save();
request()->session()->flash('success', 'Password changed!');
return redirect()->route('password.change');
}
This will validate the input, then properly save the new password for the current user. It also flashes a message and then returns them to the password change form. You can remove that part and do your own thing there.
这将验证输入,然后正确保存当前用户的新密码。它还会闪烁一条消息,然后将它们返回到密码更改表单。您可以删除该部分并在那里做自己的事情。
This utilizes a custom validation rule I created called current_password
. You must add this to your AppServiceProvider::boot()
method like so:
这利用了我创建的名为current_password
. 您必须将其添加到您的AppServiceProvider::boot()
方法中,如下所示:
public function boot()
{
// current password validation rule
Validator::extend('current_password', function ($attribute, $value, $parameters, $validator) {
return Hash::check($value, Auth::user()->password);
});
}
Just make sure you're using the Auth
, Hash
, and Validator
facades in the AppServiceProvider
.
只要确保你使用的Auth
,Hash
和Validator
外墙的AppServiceProvider
。
Lastly, we simply need to declare our error message in our lang/en/validation.php
file:
最后,我们只需要在我们的lang/en/validation.php
文件中声明我们的错误消息:
'current_password' => "The :attribute is invalid.",
Hope this helps.
希望这可以帮助。
回答by EddyTheDove
I think your $curPassword variable is empty, that's why it's always returning false. Try this way
我认为您的 $curPassword 变量是空的,这就是它总是返回 false 的原因。试试这个方法
$curPassword = $request->curPassword;
$newPassword = $request->newPassword;
Unless you send data in an input
array. But somewhoe, Hash::check()
is failing, meaning there is no match.
除非您在input
数组中发送数据。但是有人Hash::check()
失败了,这意味着没有匹配项。
As for the same string, yes, Hash::make()
always returns a different string. I guess for security purposes.
对于同一个字符串,是的,Hash::make()
总是返回不同的字符串。我猜是出于安全目的。
回答by Amr Aly
It should be
它应该是
$curPassword =$request->input('curPassword');
$newPassword = $request->input('newPassword');
not
不是
$curPassword = $request->input['curPassword'];
$newPassword = $request->input['newPassword'];
In Laravel Documentationthey are saying if you want to change the password what you could do after the checking is
在 Laravel文档中,他们说如果您想更改密码,检查后可以做什么
$request->user()->fill([
'password' => Hash::make($request->newPassword)
])->save();
回答by Sachin Kumar
There is also error in this line
这一行也有错误
$obj_user = User::find($user_id)->first();
it should be
它应该是
$obj_user = User::find($user_id);
As official documentation say
正如官方文件所说
// Retrieve a model by its primary key...
$flight = App\Flight::find(1);
// Retrieve the first model matching the query constraints...
$flight = App\Flight::where('active', 1)->first();