Saroj
Saroj

Reputation: 1373

how can I check my old password with new password

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.

Upvotes: 1

Views: 1689

Answers (1)

Mozammil
Mozammil

Reputation: 8750

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)]
                );
}

Upvotes: 1

Related Questions