问题
I am using bcrypt() function for storing my password of a user. Now if a user wants to change his/her password, then he/she will put his old password and I will check that with old password.
Problem is whenever I am using bcrypt function to the user inputed password it shows some new generated password.
Ex: During registration a user registered with 111111 password. And during change password the user also inputing 111111 but the both comes different.
Q: Why it shows different. I am using this in laravel 5.4.
回答1:
You can use Hash::check()
to check the old password against the one you have in your database as such
if (Hash::check($oldPassword, $passwordFromDb))
{
// it is a match
}
As such, an example implementation would be:
$oldPassword = $request->input('old-password');
$newPassword = $request->input('new-password');
$hashedPassword = Auth::user()->password;
if (Hash::check($oldPassword, $hashedPassword))
{
$user = User::find(Auth::user()->id)
->update(
['password'=> Hash::make($newPassword)]
);
}
来源:https://stackoverflow.com/questions/44153139/how-can-i-check-my-old-password-with-new-password