Laravel not logging in users

Ran into this little demon of a problem while using the Laravel built in authentication. Basically what was happening was that after I authenticated the user, it would show that it was valid and then nothing would be stored in the session. This is pretty much stock boiler plate code so of course I spent hours debugging it.

$credentials = $request->only('name', 'password');
if(Auth::attempt($credentials)) {
    $request->session()->regenerate();
    return redirect()->intended('/account')->with('success', 'Logged in successfully.');
}

What it came down to was that when I created my user model I used a different field name for the ID column without noting that in the model by setting the $primaryKey property for my User model to the new ID column name. I am never a big fan of using “id” for the primary keys in tables as I have always been told to name things in a way that they are uniquely identifiable. This has saved me many times and something I just keep on doing.

public $primaryKey = 'userId';

Hope that this helps anyone else that is having a problem with Laravel authenticating users but setting the session variables / cookies.

Leave a comment