laravel/framework · error · RuntimeException
Cookie jar has not been set.
Error message
Cookie jar has not been set.
What it means
Thrown by SessionGuard::getCookieJar() when the cookie jar was never injected. The guard needs it to queue/read remember-me cookies during login/logout and logoutOtherDevices(); createSessionDriver() normally calls setCookieJar() for you.
Source
Thrown at src/Illuminate/Auth/SessionGuard.php:932
*/
public function setRememberDuration($minutes)
{
$this->rememberDuration = $minutes;
return $this;
}
/**
* Get the cookie creator instance used by the guard.
*
* @return \Illuminate\Contracts\Cookie\QueueingFactory
*
* @throws \RuntimeException
*/
public function getCookieJar()
{
if (! isset($this->cookie)) {
throw new RuntimeException('Cookie jar has not been set.');
}
return $this->cookie;
}
/**
* Set the cookie creator instance used by the guard.
*
* @param \Illuminate\Contracts\Cookie\QueueingFactory $cookie
* @return void
*/
public function setCookieJar(CookieJar $cookie)
{
$this->cookie = $cookie;
}
/**
* Get the event dispatcher instance.View on GitHub (pinned to bd6b5437e6)
Solutions
- Resolve the guard through Auth::guard() (or auth()) so AuthManager::createSessionDriver() wires the cookie jar.
- If you construct the guard manually, call $guard->setCookieJar(app('cookie')).
- Avoid remember-me/login flows inside queue workers; if needed, bind a cookie factory first.
- In tests, use Illuminate\Foundation\Testing\TestCase which boots the HTTP stack.
Example fix
// before
$guard = new \Illuminate\Auth\SessionGuard('web', $provider, app('session.store'));
$guard->login($user); // throws - no cookie jar
// after
$guard = new \Illuminate\Auth\SessionGuard('web', $provider, app('session.store'));
$guard->setCookieJar(app('cookie'));
$guard->setDispatcher(app('events'));
$guard->login($user); Defensive patterns
Strategy: validation
Validate before calling
$guard = Auth::guard('web');
if (! method_exists($guard, 'getCookieJar') || ! app()->bound('cookie')) {
throw new RuntimeException('Session guard requires a cookie jar; resolve via Auth::guard() in an HTTP context.');
} Type guard
function guardHasCookieJar($guard): bool
{
try {
return $guard instanceof \Illuminate\Auth\SessionGuard
&& app()->bound('cookie');
} catch (\Throwable) {
return false;
}
} Try / catch
try {
Auth::guard('web')->login($user, true);
} catch (\RuntimeException $e) {
if (str_contains($e->getMessage(), 'Cookie jar has not been set')) {
Auth::guard('web')->setCookieJar(app('cookie'));
Auth::guard('web')->login($user, true);
} else {
throw $e;
}
} Prevention
- Always resolve session guards through Auth::guard()/auth() so the manager wires the cookie jar.
- Do not run login/logout flows inside queue workers; if unavoidable, bind the cookie factory first.
- In tests, boot the full HTTP kernel (use Illuminate\Foundation\Testing\TestCase).
When it happens
Trigger: Instantiating a SessionGuard manually (or via a custom driver) and invoking login()/logout()/logoutOtherDevices() without calling setCookieJar(); using a session guard inside a queue worker or console command where the 'cookie' binding is absent.
Common situations: Running authentication flows in a job/worker/test where the HTTP kernel did not boot the cookie middleware; custom guard setup that bypasses AuthManager::createSessionDriver(); mocking the guard and forgetting the cookie dependency.
Related errors
- Auth guard [{$name}] is not defined.
- Auth driver [{$config['driver']}] for guard [{$name}] is not
- Authentication user provider [{$driver}] is not defined.
- Password resetter [{$name}] is not defined.
- The given password does not match the current password.
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/76075dd8ec67e636.json.
Report an issue: GitHub.