在Laravel 5.6中的哈希函数中更改密码 [英] Change Password in hashed function in laravel 5.6
本文介绍了在Laravel 5.6中的哈希函数中更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改保存时被散列的密码.
I want to change my password which is been hashed while saving.
如何更改密码?
'password' => Hash::make($data->password).
我的控制器
$request->validate([
'oldpass' => 'required',
'password' => 'required|alphaNum|min:6',
'password_confirmation' => 'required|same:newpass',
]);
$id = $request->id;
$users = Auth::user()->whereId($id)->get();
foreach ($users as $user) {
if ($oldpass == $user->password) {
$user->update([
'password' => Hash::make($request->newpass)
]);
return view('\balance');
} else {
return 'error';
}
}
推荐答案
您应使用Hash::check($old_password, $hashed_password)
,如下所示:
public function passwordChange(Request $request, User $user_name) {
// find the loggedin user
$user = User::find(Auth::user()->id);
// validate rules
$validator = Validator::make($request->all(), [
'old_password' => 'required|min:6',
'password' => 'required_with:password_confirmation|required|min:6',
'password_confirmation' => 'confirmed|required|min:6',
]);
// what to do if validator fails
if ($validator->fails()) {
return redirect($user->user_name . '/settings')->withErrors($validator)->withInput();
} else {
$old_password = $request->input('old_password');
$new_password = $request->input('password');
$hashed_password = Auth::user()->password;
// checking the old pass with new one
if (Hash::check($old_password, $hashed_password)) {
$user->update([
'password'=> Hash::make($new_password)
]);
return redirect($user->user_name . '/settings')->with('success', 'Your Password updated.');
} else {
return redirect($user->user_name . '/settings')->with('success', 'Your Old password is wrong!');
}
}
}
也请注意验证器上的'password' => 'required_with:password_confirmation
和'password_confirmation' => 'required|same:newpass'
.希望对您有所帮助.
Please also notice 'password' => 'required_with:password_confirmation
and 'password_confirmation' => 'required|same:newpass'
on validator. Hope it helps.
这篇关于在Laravel 5.6中的哈希函数中更改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文