Search code examples
laravellaravel-authentication

What is the difference between @guest('admin') and @guest?


From Laravel's documentation:

@guest
    // The user is not authenticated...
@endguest
@guest('admin')
    // The user is not authenticated...
@endguest

Solution

  • You may specify which guard is being used when checking if the user is authenticated.

    From the authentication docs:

    At its core, Laravel's authentication facilities are made up of "guards" and "providers". Guards define how users are authenticated for each request. For example, Laravel ships with a session guard which maintains state using session storage and cookies.

    So, when using @guest by itself, it'll check the authentication status against the default guard.

    Alternatively, you may add custom guards if needed. The example config/auth.php shown in the documentation includes an example of how to add a custom guard:

    'guards' => [
        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
    ],
    

    If you need to use the custom guard, then you'd use @guest('api') instead (along with updating your config and creating your custom authentication guard as shown in the documentation).

    This blog post goes into more detail for how you might set things up (if you needed it).