how can I check my old password with new password

放肆的年华 提交于 2021-01-28 07:56:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!