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- Prism
@csrfemits a hidden_tokenfield fromcsrf_token
Accepted sources for the token:
- Form field
_token(from@csrf) - Header
X-CSRF-TOKEN - Header
X-XSRF-TOKEN
Mismatch raises 419 (TokenMismatchError).
<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.