Skip to content

fix(inertia): send csrf token with visits - #36

Merged
NexPB merged 1 commit into
mainfrom
fix/inertia-v3-csrf-header
Jul 9, 2026
Merged

fix(inertia): send csrf token with visits#36
NexPB merged 1 commit into
mainfrom
fix/inertia-v3-csrf-header

Conversation

@NexPB

@NexPB NexPB commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Summary

Inertia v3 replaced axios with its own XHR client, which doesn't send Phoenix's CSRF token — so any non-GET Inertia visit (useForm post, router.delete, …) would 403 on protect_from_forgery. Latent today only because the demo pages have no forms.

Ports the fix from NexPB/cloak (493c3d52 / 17e58336), minus the Clerk-specific parts:

  • assets/src/lib/http.ts (new) — installHttpHeaderInterceptor() reads the csrf-token meta tag (already rendered in root.html.heex) and attaches it as x-csrf-token on every Inertia request via http.onRequest
  • assets/src/app.tsx — installs the interceptor before createInertiaApp
  • assets/e2e/demo.spec.ts — asserts the Inertia XHR on Demo A → B navigation carries the x-csrf-token header

Not ported (already covered here): server-side :inertia is at 3.0.0-rc3, client at ^3.0.0, no axios dependency.

Testing

CI is Elixir-only, so JS was verified locally:

  • pnpm lint
  • pnpm exec tsc --noEmit
  • pnpm build
  • pnpm test:e2e — 6/6 passed, including the new CSRF header assertion
  • mix precommit — compile, format, credo strict, 4 tests passed

🤖 Generated with Claude Code

Inertia v3 replaced axios with its own XHR client, which does not send
Phoenix's CSRF token. Any non-GET Inertia visit would 403 on
protect_from_forgery. Attach the token from the csrf-token meta tag via
http.onRequest, ported from the same fix in NexPB/cloak.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@NexPB
NexPB merged commit 4442dab into main Jul 9, 2026
2 checks passed
@NexPB
NexPB deleted the fix/inertia-v3-csrf-header branch July 9, 2026 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant