Fix "419 Page Expired" in Laravel (CSRF token mismatch)
419 | Page Expired
- {"message": "CSRF token mismatch."}
- Illuminate\Session\TokenMismatchException: CSRF token mismatch.
- POST https://example.com/login 419 (unknown status)
- Request failed with status code 419
Laravel rejected a POST, PUT, PATCH or DELETE request because the CSRF token is missing or does not match the one stored in the session. Add @csrf to your forms, send the X-CSRF-TOKEN header in AJAX calls, and check that the session cookie is kept.
What this error means
To block CSRF attacks, Laravel generates a secret token per session and requires it to be sent back with every request that changes data. The ValidateCsrfToken middleware (VerifyCsrfToken before Laravel 11) compares the token it receives, through the _token field, the X-CSRF-TOKEN header or X-XSRF-TOKEN, with the one in the session. If they differ, it throws a TokenMismatchException, rendered as a 419 "Page Expired" page or as {"message": "CSRF token mismatch."} for a JSON request. The token may be missing, but the session itself may also have expired or never been stored.
Common causes
- Missing
@csrfin a Blade form that sends aPOSTrequest. - Expired session: the page stayed open longer than
SESSION_LIFETIME(120 minutes by default), or sessions were cleared. - AJAX request without a CSRF header:
fetchdoes not send the token on its own, unlike axios which reads theXSRF-TOKENcookie on the same domain. - Session cookie not stored: wrong
SESSION_DOMAIN,SESSION_SECURE_COOKIE=trueover HTTP,storage/framework/sessionsnot writable, or a changedAPP_KEY. - Misconfigured SPA with Sanctum: missing call to
/sanctum/csrf-cookie, or front-end domain missing fromSANCTUM_STATEFUL_DOMAINS.
Step-by-step fix
-
Add
@csrfto the formEvery Blade form that is not
GETmust contain@csrf, which outputs a hidden_tokenfield. ForPUT,PATCHorDELETE, add@methodtoo.<form method="POST" action="{{ route('profile.update') }}"> @csrf @method('PUT') <input type="text" name="name" value="{{ old('name', $user->name) }}"> <button type="submit">Save</button> </form> -
Send the token in AJAX requests
Put
<meta name="csrf-token" content="{{ csrf_token() }}">in your layout’s<head>, then read it and send it in theX-CSRF-TOKENheader.const token = document.querySelector('meta[name="csrf-token"]').content; await fetch('/profile', { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': token, }, body: JSON.stringify({ name: 'Alice' }), }); -
Check that the session cookie is kept
In DevTools (Application > Cookies), the session cookie (
laravel_sessionby default) andXSRF-TOKENmust exist and stay the same between requests. If not, check.envthen clear the config cache withphp artisan config:clear.SESSION_DRIVER=database SESSION_LIFETIME=120 # leave empty for the current host, or ".example.com" for subdomains SESSION_DOMAIN= # true only when the site is served over HTTPS SESSION_SECURE_COOKIE=false -
Make the sessions folder writable
With
SESSION_DRIVER=file, PHP must be able to write tostorage. On Linux, give ownership to the web server user (oftenwww-data) instead of usingchmod 777. WithSESSION_DRIVER=database, thesessionstable must exist (php artisan migrate).sudo chown -R www-data:www-data storage bootstrap/cache sudo chmod -R 775 storage bootstrap/cache -
SPA with Sanctum: initialize the CSRF cookie
Call
/sanctum/csrf-cookiebefore logging in, enable cookies and the XSRF token in axios, and add the front-end domain toSANCTUM_STATEFUL_DOMAINS(for examplelocalhost:5173). Both apps must share the same top-level domain.import axios from 'axios'; axios.defaults.withCredentials = true; axios.defaults.withXSRFToken = true; await axios.get('http://localhost:8000/sanctum/csrf-cookie'); await axios.post('http://localhost:8000/login', { email, password }); -
Handle session expiry gracefully
For a session that really expired, send the user back to the form with a message instead of the raw 419 page (Laravel 11+, in
bootstrap/app.php).use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Http\Request; use Illuminate\Session\TokenMismatchException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; ->withExceptions(function (Exceptions $exceptions) { $exceptions->render(function (Throwable $e, Request $request) { $expired = $e instanceof TokenMismatchException || ($e instanceof HttpExceptionInterface && $e->getStatusCode() === 419); if ($expired && ! $request->expectsJson()) { return back() ->withInput($request->except('password', '_token')) ->with('error', 'Your session expired. Please try again.'); } }); })
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Use a shared layout with the
csrf-tokenmeta tag and a central HTTP client that adds the header automatically. - Only exclude a route from CSRF checks (
$middleware->validateCsrfTokens(except: ['stripe/*'])) for signed external webhooks, never for your own forms: that would reopen the door to CSRF attacks. - Do not cache (CDN, full-page cache) pages that contain a form: the token served would belong to another session.
Frequently asked questions
Why do I get a 419 error on the login form only in production?
Most often the session cookie is not stored: SESSION_DOMAIN or SESSION_SECURE_COOKIE do not match the real domain or protocol, or an HTTPS proxy is not declared as a trusted proxy.
Should I disable CSRF protection to fix the 419 error?
No. It stops third-party sites from sending requests on your behalf. Fix how the token or session is sent; only exclude external webhooks that have their own signature.
How do I extend the time before "Page Expired"?
Increase SESSION_LIFETIME (in minutes) in .env, then run php artisan config:clear. A very long lifetime increases the risk if a device stays logged in.
Useful tools for this error
More PHP & Laravel errors
- Fix "Allowed memory size of 134217728 bytes exhausted" in PHP
- Fix "Target class [X] does not exist" and "Class not found" in Laravel
- Fix "SQLSTATE[HY000] [2002] Connection refused" in Laravel
- Fix "No application encryption key has been specified" in Laravel
- Fix "Your requirements could not be resolved to an installable set of packages" (Composer)
- Fix "laravel.log could not be opened in append mode: Permission denied"
- Fix "Vite manifest not found at: public/build/manifest.json" in Laravel