Determining if No User is Logged In

Problem

You want to see if there is not a user logged in.

You know Laravel automatically keeps the authenticated user in the session. You want to check if the current request doesn't have a user logged in.

Solution

Use Auth::guest().

The Auth::guest() method returns true or false.

if (Auth::guest())
{
    echo "Bummer! You need to log in, dude.";
}

Discussion

Auth::guest() complements Auth::check().

It is the exact opposite. In fact, here's how Auth::guest() is actually implemented.

    public function guest()
    {
        return ! $this->check();
    }

See Determining if the Current User is Authenticated.

The 'auth' filter uses this method

Laravel provides a default implementation of the auth filter in app/filters.php.

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest('login');
});

This default implementation is used when you want to add a filter to a route that ensures the route is only accessed by logged in users. Since Auth::guest() returns true if no user is logged on, then this implementation simply redirects the user to your application's login page.

comments powered by Disqus