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

wirelessm's avatar

Angular 4/5 and Laravel 5.5 XSRF-TOKEN

I'm currently working in a web where i use Angular 5 and Laravel 5.5 i am having a trouble on implementing XSRF-TOKEN, On the documentation of Laravel 5.5 XSRF-TOKEN laravel send XSRF-TOKEN inside the header on Angular i get the token and when i going to make POST request i put X-XSRF-TOKEN header which contain the XSRF-TOKEN that i get. But it still give me an error and i tried to find where the error came and that lead me to the tokensMatch() inside VerifyCsrfToken Class and i found out that when they matching the decrypted XSRF-TOKEN and $requrest->session()->token() using hash_equals() it returns false that's why i am receiving TokenMismatchException.

Is there a way to resolve this? if yes how. Thank you.

0 likes
7 replies
Paschal's avatar

Put your token in your meta tag of your header (If that doesn't exist yet). Configure your ajax to always pick the token from header so you don't have to include it in every ajax result.

wirelessm's avatar

Thank you @Paschal for the suggestion. I use interceptor on Angular I put the xsrf-token on my post request, my problem is when the token is verifying I am getting a false on hash_equals() but the xsrf-token that I send is the xsrf-token that I got on response cookie that laravel set

wirelessm's avatar

@kvalbot Sorry I haven't find any solution. I use laravel passport also it's look like we can't implement xsrf protection

kvalbot's avatar

Are your Angular and Laravel apps on the same domain? Cookies can only be consumed from the same domain

Please or to participate in this conversation.