Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sunrise's avatar

What does `Auth::guard()` mean?

I am using Laravel 5.3.

What does Auth::guard() mean?

protected function guard()
    {
        return Auth::guard();
    }
0 likes
2 replies
kevinvdburgt's avatar
Level 4

Take a look at config/auth.php.

You can create multiple guards for example, you have a database table users and admins you can define them in the config/auth.php and use:

Auth::guard('admin')->user() and Auth::guard('user')->user()

By default your config has been set on web when using the web guard. If that is the only guard you have you can just use Auth::user() instead of Auth::guard('web')->user()

6 likes
redbonzai's avatar

The authenticate middleware allows you to specify what type of auth guard you want to use. Laravel 5.3 comes with two out of the box, 'web' and 'api'.

The Auth facade uses the 'web' guard by default if none is specified. So for example: Auth::user() is doing this by default: Auth::guard('web')->user()

The other auth driver out of the box called 'api'. So for example you can call your middleware like this: $this->middleware('auth:api');

This will check that the user is authenticated by api_token instead of session. Then you can get an instance of the user by Auth::guard('api')->user() instead of Auth::user() which is the same as Auth::guard('web')->user()

You would use this if your application has an API endpoint allowed for logged in users only. then a user can make a request like yourapp.com/api-method?api_token=blahblah. Then you can use the 'api' auth guard to authenticate and grab the logged in User.

I found this tutorial pretty useful in setting it up: http://learninglaravel.net/multiple-authentication-guard-drivers-including-api-in-laravel-52

And to answer your second question. Yes guest() is just the opposite of check(). check out laravel\framework\src\Illuminate\Auth\GuardHelpers.php

8 likes

Please or to participate in this conversation.