简体   繁体   中英

How do I check if Bcrypt password is correct?

I was using md5 to hash my passwords but learned that using bcrypt was more secure.

When using md5, it was easy to check whether a password entered in a form was correct. I simply done

if(md5($request->password) == $user->password)
   //Login or whatever

So how do I do this using bcrypt? I tried

if(bcrypt($request->password) == $user->password)

But that isn't working.

Use the attempt() method:

if (Auth::attempt(['email' => $email, 'password' => $password]))

The attempt method accepts an array of key/value pairs as its first argument. The values in the array will be used to find the user in your database table.

https://laravel.com/docs/5.4/authentication#authenticating-users

Under the hood attempt() uses password_verify() method to check password.

You could also use the check method of the Hash Facade

if (Hash::check($request->password, $user->password)) {
    // The passwords match...
}

https://laravel.com/docs/5.4/hashing#basic-usage

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