Anas Kasmi
Anas Kasmi

Reputation: 43

Can i check if a hashed password is equal to a specific value in laravel?

in my application, I have a default password (1234 for example) for all users I create, when the user login for the first time he will be asked to change that password

my goal is when a user login I want to check if his password equal to that default value (1234) if that's true I redirect him to the reset page if not I 'll do nothing

so my question is how to check that user's password if it's equal or not to a value I have?

Upvotes: 2

Views: 1163

Answers (1)

Anas Kasmi
Anas Kasmi

Reputation: 43

I have found an answer on StackOverflow that helped me a lot You can use it a couple of ways:

Out of the container

$user = User::find($id);  
$hasher = app('hash');  
if ($hasher->check('passwordToCheck', $user->password)) 
{
    // Success  
}

Using the Facade

$user = User::find($id);
if (Hash::check('passwordToCheck', $user->password)) 
{
    // Success   
}

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    
}

post url

Upvotes: 2

Related Questions