Check whether password is correct or not in Laravel

you can use hash:check method.

create password using hash:

$password = Hash::make('secret');

check password:

if (Hash::check('secret', $hashedPassword))
{
    // The passwords match...
}

As Hiren has mentioned you can use the default registered hasher as that is passed to the specific UserProvider used. The default is Illuminate\Hashing\BcryptHasher.

You can use it a couple of ways:

  1. Out of the container
$user = User::find($id);
$hasher = app('hash');
if ($hasher->check('passwordToCheck', $user->password)) {
    // Success
}
  1. Using the Facade
$user = User::find($id);
if (Hash::check('passwordToCheck', $user->password)) {
    // Success
}
  1. Out of interest using the generic php function password_verify also works. However that works because the default hashing algorithm it uses is bcrypt.
if (password_verify('passwordToCheck', $user->password)) {
    // Success
}

When the user attempts to access the page, redirect them to an auth page.

Do the ajax call then do the following in your php:

public function check(Request $request)
{
    if(Hash::check($request->password, $user->password)) {
        // They match
    } else {
        // They don't match
    }
}

I havn't tested this so it might not work.

Tags:

Laravel