Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Checking passwords before manually logging in

I'm getting problem with manually logging in users using Auth::loginUsingId . Before I do that I need to check if password matches and there is my problem. Password comparision is always returning false;

That's how I save password to DB:

    $user = User::create([
        'password' => Hash::make(Input::get('password')),
    ]);

And that's how I try to compare these two passwords

$u = User::where('username', $username)->first();    
$password = Hash::make(Input::get('password'));
var_dump($u->password == $password);

And this is returning false, and I don't know where the problem is.

EDIT: In DB field varchar is 70 char long, hash is 60 characters long

Ok I've noticed: that every time Hash::make gives another string from my password. How it's even possible? I mean how then Auth::attempt works?

like image 986
Silverfall05 Avatar asked Dec 26 '13 11:12

Silverfall05


People also ask

How do I log in manually Laravel?

Manually Logging In A User If you need to log an existing user instance into your application, you may call the login method with the user instance: Auth::login($user); This is equivalent to logging in a user via credentials using the attempt method.

How does Laravel match hashed password?

From Laravel 5 onward, you can use the bcrypt() function to hash a plaintext. So, you can save that hashed password in DB and then, compare the hashed password again to match. $save_password = bcrypt('plain_text_password'); $check_password = bcrypt('provided_password_while_login_request'); And then, compare these two.


1 Answers

Try the following:

$user = User::where('username', $username)->first();  

if (Hash::check(Input::get('password'), $user->password))
{
    // The passwords match...
}
like image 141
Anam Avatar answered Oct 11 '22 05:10

Anam