laravel/framework · error · InvalidArgumentException

Auth guard [{$name}] is not defined.

Error message

Auth guard [{$name}] is not defined.

What it means

Thrown by AuthManager::resolve() when getConfig($name) returns null, i.e. there is no 'auth.guards.{name}' key in config/auth.php. The manager cannot build a guard it has no configuration for.

Source

Thrown at src/Illuminate/Auth/AuthManager.php:90

        $name = enum_value($name) ?: $this->getDefaultDriver();

        return $this->guards[$name] ??= $this->resolve($name);
    }

    /**
     * Resolve the given guard.
     *
     * @param  string  $name
     * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
     *
     * @throws \InvalidArgumentException
     */
    protected function resolve($name)
    {
        $config = $this->getConfig($name);

        if (is_null($config)) {
            throw new InvalidArgumentException("Auth guard [{$name}] is not defined.");
        }

        if (isset($this->customCreators[$config['driver']])) {
            return $this->callCustomCreator($name, $config);
        }

        $driverMethod = 'create'.ucfirst($config['driver']).'Driver';

        if (method_exists($this, $driverMethod)) {
            return $this->{$driverMethod}($name, $config);
        }

        throw new InvalidArgumentException(
            "Auth driver [{$config['driver']}] for guard [{$name}] is not defined."
        );
    }

    /**

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Add the missing guard entry under 'guards' in config/auth.php with a valid 'driver' and 'provider'.
  2. Check the value of auth.defaults.guard and your AUTH_GUARD env variable.
  3. Verify the guard name spelling in middleware: Route::middleware('auth:web').
  4. Run php artisan config:clear after editing config/auth.php.

Example fix

// before - config/auth.php
'guards' => [
    'web' => ['driver' => 'session', 'provider' => 'users'],
],
// Auth::guard('admin') throws

// after
'guards' => [
    'web' => ['driver' => 'session', 'provider' => 'users'],
    'admin' => ['driver' => 'session', 'provider' => 'admins'],
],
Defensive patterns

Strategy: validation

Validate before calling

$guard = 'admin';
if (is_null(config("auth.guards.{$guard}"))) {
    throw new RuntimeException("Guard [{$guard}] is not configured in auth.guards.");
}
Auth::guard($guard)->user();

Type guard

function guardIsConfigured(string $guard): bool
{
    return ! is_null(config("auth.guards.{$guard}"));
}

Try / catch

try {
    return Auth::guard($name)->user();
} catch (\InvalidArgumentException $e) {
    // fall back to default guard or abort with a clear config error
    return Auth::guard()->user();
}

Prevention

When it happens

Trigger: Calling Auth::guard('admin') when 'admin' is not listed under auth.guards; accessing Auth::user() when auth.defaults.guard points to a guard name that was removed or renamed.

Common situations: Renaming a guard but forgetting to update AUTH_GUARD env; typo in the guard name passed to the auth middleware (Route::middleware('auth:admins')); a package that expects a guard you never published.

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/976fb1007f4f4439.json. Report an issue: GitHub.