Getting the Currently Cached Authenticated User

Problem

You want to get the currently cached authenticated user.

But if there is no user cached, you do not want to attempt authentication.

Solution

Use the Auth::getUser() method.

$user = Auth::getUser();
if ( ! $user)
{
    echo "No user is currently authenticated.";
}

Discussion

The Auth::getUser() and Auth::user() methods are slightly different.

That Auth::user() will attempt to authenticate a user from the session or the "remember me" cookie if there is not a user cached.

Auth::getUser() only returns a user if they've already been authenticated successfully.

comments powered by Disqus