Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 authentication not working

I have a Laravel 4 app in which I have set up one user. In my login route I'm calling Auth::attempt with the email and password but it always comes back as false. I definitely have the password correct and the correct hash in the database as Hash::check returns true.

I think it may be due to using email as the login field instead of username, but I can't see any setting for that. This question implied you could add an option to config/auth.php but it didn't work. This question says to use username as the array key, but then I get SQL error because it tries to select on a username field in the database.

Do I need to add something to the User model to specify the username field? Here is my login route:

Route::post('login', function() {

    // data from login form
    $credentials = array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
    );

    $auth = Hash::check(Input::get('password'), Hash::make('mypass'));
    var_dump($auth); // this is TRUE

    // login was good
    $auth = Auth::attempt($credentials);
    var_dump($auth); // this is FALSE
});
like image 684
DisgruntledGoat Avatar asked Apr 24 '13 00:04

DisgruntledGoat


3 Answers

I found the problem. As suggested by Jason in the comment above, I had modified the models/User.php file and removed some functions that I didn't realise were necessary.

The getAuthIdentifier() and getAuthPassword() methods must be included in the User model for authentication!

like image 165
DisgruntledGoat Avatar answered Sep 29 '22 13:09

DisgruntledGoat


In app/config/app.php make sure you have the 'key' set. This made me pull my hair out. Everything will apear to work, password seems hashed in the DB, but it will always return false until you set this key and re-hash your password into the DB.

"php artisan key:generate"

like image 35
Neil Holcomb Avatar answered Sep 29 '22 14:09

Neil Holcomb


Had the same problem and made me sweat for hours. Definitively check your User.php model and make sure you have not overwritten the default one. Thanks Jason!

like image 38
Banago Avatar answered Sep 29 '22 13:09

Banago