bishtyogeshsingh's avatar

Issue: Laravel Hash Not Working Correctly

I am using Laravel 11 and working on the login functionality. During the registration process, I hash the password, like this:

$validDationData['password'] = Hash::make($validDationData['password']);

For example, if the plain password is "Yogesh", the hashed password saved in the database looks like this:

$2y$12$X4C08AqVPCT6NBRTBecbIO0Q6SlddwzOYinAcTcNm.rpV6H00N8ee

However, when I try to verify the password during login, I keep getting the "Invalid Password" error.

Here's the code I am using to check the password:

$plainPassword = "Yogesh";
echo "Plain password: " . $plainPassword . "<br>";
$storedHash = '$2y$12$X4C08AqVPCT6NBRTBecbIO0Q6SlddwzOYinAcTcNm.rpV6H00N8ee'; // Hash from the database
echo "Stored hash: " . $storedHash . "<br>";

if (Hash::check($plainPassword, $storedHash)) {
    echo 'Password matches!';
} else {
    echo 'Invalid password.';
}
die();

Despite using the correct plain password and hash, I always get "Invalid Password."

0 likes
5 replies
Shivamyadav's avatar

I think in your user model there is a casts array methods which is make the password hashed by default with bcrypt method. Disable that and try again

Shivamyadav's avatar

Ensure that the hashing driver used during registration (Hash::make) and login (Hash::check) are the same.

Check Your config/hashing.php file 'default' => 'bcrypt', By default, Laravel uses bcrypt. If you change this to another algorithm, or if the hash was created with a different driver, Hash::check will fail.

bishtyogeshsingh's avatar

Everything is working fine now. The issue was that I was using Hash::make() twice, which was causing the error.

Please or to participate in this conversation.