MadMatt
MadMatt

Reputation: 119

Password verify always returns fail in Laravel (Hash::check)

I've got an issue when I try to validate the post password via Hash::check in Laravel 5.5

I made a posts table (in this case sales table) with password column. When I try to create the post, it's working perfectly and the password is hashed and also belongs to logged in User. Then on the current post page is a button with an input (password) to delete that specific post, but the condition is always false.

My Controller public function destroy(Request $request, $id)

$input_pass = request('input_password');

    $sale = Sale::find($id);
    $hashed = $sale->password;

    // Check if sale password is correct
    if (Hash::check($input_pass, $sale->password)) {

    $sale->delete();

    } else {
        // something else to do
    }

For the post store, I used bcrypt method to hash the password. I've been also trying to dd('sale->password') which refers to column in sales table (correct) and dd('$input_pass') which refers to typed in password in DELETE form (also correct) - so I'm a little bit confused, why the pass don't match.

Upvotes: 1

Views: 1173

Answers (1)

Sapnesh Naik
Sapnesh Naik

Reputation: 11656

From your comment I find that you have a logical error where you initially hash your password and persist it in DB.

You are passing the string password to bcrypt where it should actually be something like request('password')

Change

'password' => bcrypt('password'),

to

'password' => bcrypt(request('password')),

Upvotes: 3

Related Questions