Auth guard [admin] is not defined
You are staring at a 401 Unauthorized error on your admin dashboard, and the stack trace clearly states auth guard [admin] is not defined. You have double-checked your config/auth.php file; the 'admin' key exists, pointing to a session driver and an admins provider. Your Admin model extends Authenticatable, sets $guard = 'admin', and you have explicitly attached the auth:admin middleware to your controller's constructor. Logic dictates everything should work.
The disconnect lies in how Laravel resolves route guards versus how it handles custom authentication guards defined outside the standard defaults. This is a common stumbling block when moving beyond the default "web" guard, specifically when using older Laravel versions where the routing mechanism hasn't been updated to automatically recognize custom guards for specific routes.
When you define Auth::routes() in your web.php file, Laravel generates registration and login routes. However, these routes are hard-coded to use the web guard unless explicitly told otherwise. More critically, when you attempt to access a route like /admin without a dedicated login route for that specific guard, or if you rely on middleware that expects the guard to be resolved by name before the request hits your controller, Laravel throws this exception if it cannot find a matching configuration entry in the guards array at the moment of resolution.
In many versions of Laravel, simply defining the guard in config/auth.php is necessary but not always sufficient if the route group or the middleware stack isn't explicitly configured to use that guard name. The error suggests that when the authentication system tries to validate the session, it looks for a guard named admin, but the internal mapping fails because the configuration wasn't loaded into the active request context correctly, or the route hasn't been registered with that specific guard intent.
To resolve this without rewriting your entire application, you need to ensure the middleware stack explicitly handles the guard name. If you are running an older version of Laravel (pre-5.7), the auth:admin middleware syntax might not be fully supported in the way you expect for route groups. In modern Laravel, the fix often involves ensuring that the Route::group directive wraps your admin routes and explicitly defines the guard.
Here is a robust pattern to apply to your routes/web.php:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Backend\AdminController;
// Define the admin route group with the specific guard
Route::middleware('auth:admin')->group(function () {
Route::get('/admin', [AdminController::class, 'index'])->name('admin.dashboard');
});
Alternatively, if you are using Auth::routes() to generate your login flow for admins, you must specify the guard name within that method call:
Auth::routes(['register' => false, 'reset' => false]); // Keep defaults for web
// Or specifically for admin if creating separate routes:
Route::get('/admin/login', 'Backend\AdminLoginController@showLoginForm')->middleware('guest:admin');
Route::post('/admin/login', 'Backend\AdminLoginController@login')->name('admin.login');
The critical takeaway is that the guard name must be consistent across three places: the configuration file, the model's $guard property, and the middleware/route definition. If your AdminController uses $this->middleware('auth:admin'), but your route group above doesn't wrap it or you haven't registered a login route for admin, Laravel might default to checking the web guard first, causing the mismatch if the session isn't set up for admin.
Another potential pitfall involves the RedirectIfAuthenticated middleware. In your app/Http/Middleware/RedirectIfAuthenticated.php, you must ensure this file checks for the correct guard names defined in your config. If this middleware only checks $guard = 'web' by default, it will force a redirect to the web home page for admin users, effectively bypassing their intended dashboard and potentially triggering unexpected behavior that manifests as authentication errors.
// In RedirectIfAuthenticated.php
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
// Check which guard is being used to redirect correctly
return route('admin.home'); // Ensure this route exists or use a fallback
}
}
By aligning your middleware logic with the specific guards defined in config/auth.php, you eliminate the ambiguity that triggers the "not defined" exception. This ensures that when a user attempts to access /admin, Laravel correctly routes them to the admin login page if unauthenticated, or loads the dashboard if authenticated against the admins model via the admin guard.
