Learn to master Laravel authentication guards and providers to handle multi-auth scenarios. Secure your application by isolating different user types effectively.
Previously in this course, we explored implementing middleware for API security to protect our project board resources. While basic middleware handles authorization, we often encounter scenarios where our application needs to distinguish between different types of users—like system administrators and standard project members—at the authentication level.
In Laravel, the auth.php configuration file is the heart of your security architecture. Understanding how to customize authentication guards and user providers is essential when your application outgrows a single "User" model.
In Laravel, authentication is split into two distinct responsibilities: Guards and Providers.
session (for web) and token (for APIs). They determine how the application retrieves the user (e.g., from an encrypted session cookie or a Bearer token in the header).When you need to support, for example, an admin login alongside your standard user login, you don't just add a column to the users table. You define a new guard that points to a specific provider, ensuring your security logic remains clean and isolated.
To implement a multi-auth scenario, we must modify config/auth.php. Suppose our project board needs an "Admin" entity that is stored in a separate admins table.
First, define the Provider in config/auth.php:
PHP'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], ],
Next, define the Guard that uses this provider:
PHP'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], ],
Now, your application can distinguish between the two. When an admin logs in, you specify the guard: Auth::guard('admin')->login($admin). Any subsequent check using Auth::guard('admin')->check() will specifically validate the session against the admins table.
In our project board, we might want to ensure that only admins can access the system-wide configuration routes. We use the auth middleware, but pass the guard as a parameter.
PHPRoute::middleware(['auth:admin'])->prefix('admin')->group(function () { Route::get('/dashboard', [AdminController::class, 'index']); });
This ensures that if a standard user tries to access /admin/dashboard, the admin guard will fail to find a valid session, and the user will be redirected to the configured login path.
Create a new guard named support in your config/auth.php.
SupportAgent model and migration (if you haven't already).auth.php named support_agents.support guard to use the support_agents provider.auth:support to ensure only support agents can access the ticketing interface.admin guard is used for APIs, remember that you may need to configure a different driver (like sanctum) rather than session. Mixing session-based and token-based logic within one guard is a frequent source of "401 Unauthorized" errors.By mastering guards and providers, you gain precise control over who can access which parts of your system. Remember that guards are the mechanism of entry, while providers are the source of truth. Use them to keep your administrative logic strictly separated from your user-facing features, maintaining a secure and professional architecture.
Up next: We will dive into Job Chaining and Batching to handle complex, multi-step background processes for our project tasks.
Master Laravel Policies to secure your PHP applications. Learn how to move authorization logic out of controllers into clean, reusable, and testable classes.
Customizing Authentication Guards