Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

beartown's avatar

Using XSRF-TOKEN cookie in SPA

Hi guys, XSRF-TOKEN cookie is encrypted in Laravel. How do I use it in a SPA? From the docs, I thought I could just pass it directly to X-XSRF-TOKEN header, but it doesn't work. I need to decode it.

Should I decode it on server-side and then pass it to the front-end? What's the expected workflow here?

1 like
3 replies
beartown's avatar

@vincent15000 I already use Sanctum, but I still don't understand how am I supposed to use that value from a cookie. The docs say:

During this request, Laravel will set an XSRF-TOKEN cookie containing the current CSRF token. This token should then be passed in an X-XSRF-TOKEN header on subsequent requests, which some HTTP client libraries like Axios and the Angular HttpClient will do automatically for you. If your JavaScript HTTP library does not set the value for you, you will need to manually set the X-XSRF-TOKEN header to match the value of the XSRF-TOKEN cookie that is set by this route.

I'm just using fetch() and I manually set X-XSRF-TOKEN header, but I'm getting a mismatch if I use that encrypted value. But when I put csrf_token() in my markup and then use that value in the header, it works.

1 like

Please or to participate in this conversation.