简体   繁体   中英

How to compare new password to old hashed password?

I have made a update current password API in laravel 8. It is changing my current password if I pass the new password as an argument. But problem is if I pass both passwords ie old password and new password same. It still says password updated. I want to show an error message saying password should not be same. Here is my code of change password:

public function updatePassword(Request $request)
{
    $input = $request->validate([
        'old_password'=> 'string',
        'password'=> 'string'
    ]);
    if (Hash::check($input['old_password'], auth()->user()->password)) {
        $user = new User();
        $user = auth()->user();
        $user->password = bcrypt($input['password']);
        $user->save();

        return $this->success('Password Updated', $user);
    }
    return $this->error('Password should not be same');
}`

You already have old password in input.

if($input['password'] === $input['old_password']){
    return $this->error('Password should not be same');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM