How Laravel’s authentication system works
The Auth facade and AuthManager
TheAuth facade proxies Illuminate\Auth\AuthManager. The manager uses a driver pattern to manage multiple guards and creates the appropriate guard instance based on config/auth.php.
resolve() reads the driver key from the guards array in config/auth.php and invokes the matching factory closure. The built-in session and token drivers use the same mechanism.
Guard vs StatefulGuard
Any authentication guard must implement at minimumIlluminate\Contracts\Auth\Guard. If the guard needs to maintain session state, implement StatefulGuard instead.
Guard interface (Illuminate\Contracts\Auth\Guard)
Guard interface (Illuminate\Contracts\Auth\Guard)
StatefulGuard interface (Illuminate\Contracts\Auth\StatefulGuard)
StatefulGuard interface (Illuminate\Contracts\Auth\StatefulGuard)
StatefulGuard extends Guard and adds methods for session and cookie-based login state.Guards for API or token-based authentication typically only need to implement
Guard because there is no session state to maintain. Guards that need session-based login (e.g. an admin panel) should implement StatefulGuard.Implementing a custom guard
The GuardHelpers trait
The implementations ofcheck(), guest(), id(), and hasUser() are nearly identical across all guards. Laravel provides Illuminate\Auth\GuardHelpers to handle them automatically, so you only need to implement user() and validate().
Building an API token guard
The example below follows the design of Laravel’s built-inTokenGuard. It reads a token from the Authorization header or a query parameter and resolves the user through the configured UserProvider.
1
Create the guard class
Place the guard class in
app/Auth.2
Register the guard in a service provider
Register the guard in the
boot() method of AppServiceProvider using Auth::extend().Auth::createUserProvider() reads the providers section of config/auth.php and returns the matching UserProvider instance. Unless you need a custom provider, this returns the standard EloquentUserProvider.3
Configure the guard in config/auth.php
Add the new guard to the
guards array.4
Apply the guard to routes
Pass the guard name to the To access a specific guard in controller code, use
auth middleware.Auth::guard('api') or auth('api').Closure-based request guards
Auth::viaRequest() lets you define a simple guard using only a closure — no class required. This is suitable for prototypes or very straightforward authentication needs.
config/auth.php:
Custom UserProvider
When user data comes from a source other than the database — an external API, LDAP, etc. — implementIlluminate\Contracts\Auth\UserProvider.
Registering the custom UserProvider
providers section of config/auth.php:
Practical example — separate guards for admins and users
1
Create the Admin model
The model must extend
Authenticatable to integrate with the auth system.2
Configure config/auth.php
3
Protect admin routes
Related page
Authentication (basics)
Standard session-based authentication, starter kits, and route protection.