Laravel Login Not Working, Auth::user() Returns Null?

Laravel Auth::user() is Null

Credentials validate but user never logged in. Here are some culprits.

 

When upgrading a 5.3 site to 5.5, I encountered this problem, and here are a few tips that may help you.

This problem looks like the login form redirecting back to the login page, except no error message is given. If you dump and die on Auth::user() or Auth::id(), you will get null.

Your Laravel Tables & Cols are Named Correctly

If you used Artisan’s make::auth right out of the box, then Laravel is looking for a table called users. Did you rename that table? If so, your system is unable to log a user in without a little more configuration.

On that users table you need to have a column called id that is the primary key for these records. Did you rename that column? If yes, you need to connect a few more dots because out-of-the-box, Auth is looking for id.

Using Something Other Than Email Addresses to Log In?

By default, when a user logs in via the make::auth login system, they are going to be providing their email address and password. Did you change the form to use something else, like name or username fields? You’re going to have to tell Laravel about that.

Your Users Model Has $id Property Declared

This ended up being the problem in my case, and I can’t for the life of me remember why I did this. After much tail chasing, if you have this code:

class Users extends Authenticatable {
	
    use Notifiable, SoftDeletes; 
    
    public $id; // this is going to be ants at a picnic

then you REALLY NEED to remove that public $id line. This is going to prevent your logged in user data from being set properly, and your user’s credentials will validate but never stick. Hence the endless login cycle.

Last updated on