CSRF Protection
Stateful web routes mint a CSRF token in the session and reject unsafe methods
when the token is missing or wrong. API routes stay stateless — use bearer
tokens instead of CSRF.
How it works
Section titled “How it works”StartSessionloads the signed (and encrypted) session cookieVerifyCsrfTokenensures_csrf_tokenexists and checks mutating requests- Every successful response also sets a readable
XSRF-TOKENcookie so SPA / Inertia clients can echo it asX-XSRF-TOKEN - Prism
@csrfemits a hidden_tokenfield fromcsrf_token
Accepted sources for the token:
- Form field
_token(from@csrf) - Header
X-CSRF-TOKEN(plain token, e.g. from a meta tag) - Header
X-XSRF-TOKEN(value of theXSRF-TOKENcookie — decrypts whenEncryptCookieswrapped it)
Mismatch raises 419 (TokenMismatchError).
Inertia / Vue / React / Svelte
Section titled “Inertia / Vue / React / Svelte”Official @inertiajs/* clients use axios defaults (xsrfCookieName /
xsrfHeaderName). After the first GET of a page, the browser has
XSRF-TOKEN; subsequent form.post(...) calls send X-XSRF-TOKEN
automatically. You do not need a hidden @csrf field in Vue forms.
Prism forms
Section titled “Prism forms”<form method="post" action="/login"> @csrf <input name="email" type="email"> <button type="submit">Sign in</button></form>AuthServiceProvider shares csrf_token into every view so @csrf works
without manual wiring.
Middleware group
Section titled “Middleware group”Register on the web stack (scaffold default):
middleware.web( prepend=["cookies.encrypt", "session.start", "csrf", "auth.start"], append=["locale"],)Do not put csrf on the api group.