neolaser
neolaser

Reputation: 6907

Laravel authentication issues getUser failing, check working

I have one route to login and return the logged in user which successfully returns the user model

return Auth::getUser();

I have another route that simple returns the logged in user, but it returns null

Route::get('authUser', function () {
    return Auth::getUser();
});

Finally, I have a third route that checks if the user is logged in, which returns true

return Auth::check();

How could Auth::getUser() return null, but Auth::check() return true?

Upvotes: 1

Views: 206

Answers (2)

lukasgeiter
lukasgeiter

Reputation: 152880

Both Auth::getUser() and Auth::user() exist but there is a very important difference between them. Auth::getUser() will only return a cached user. That means if the user is authenticated in the session it will still return null. Only if the user has been loaded into the cache (e.g. by calling Auth::user()) you'll get the user model returned.

That means you should use Auth::user() in your code.

Upvotes: 4

Tzook Bar Noy
Tzook Bar Noy

Reputation: 11677

I don't know

Auth::getUser()

But I think you meant:

Auth::user()

Check it out...

Upvotes: 3

Related Questions