-
Notifications
You must be signed in to change notification settings - Fork 513
CSRF token mismatch. #428
Comments
I managed to get it to work eventually. The CSRF issue suddenly disappeared. I have no idea why. |
Hey there! If a meta tag with the csrf-token as the value is found, it will be automatically passed by echo as the header: protected setOptions(options: any): any {
this.options = Object.assign(this._defaultOptions, options);
if (this.csrfToken()) {
this.options.auth.headers['X-CSRF-TOKEN'] = this.csrfToken();
}
return options;
} Inspired by: You could do it manually too: window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
authEndpoint: "/broadcasting/auth",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}); |
@tlaverdure I was just using a fresh Laravel 5.8 install. Everything should be good to go out of the box really. Thanks for helping out. |
I just discovered that this happens when I run in dev mode. In production mode it's fine. |
This issue still plagues me because everything seams fine but token isn't being sent by echo |
@driesvints please what did you do to resolve this? other than running in production mode |
No sorry. It's been too long ago. |
@mfoncho check the meta tags in your view. Do you see the CSRF token? If it is probably missing |
@willsmanley i have everything working including passport just websockets! |
The problem is cookies. The cookies aren't sent with the channel subscription payload to verify to token against. I did a reverse proxy so the websocket and host are on the same primary host domain but i'm getting some other errors. |
Echo sends the CSRF token in the request header automatically if it detects the CSRF meta tag in the DOM. Otherwise you’ll have to pass it manually as shown above by @tlaverdure |
You should just go to chrome's network tab and inspect the request sent by echo. See if it has the right cookies there. |
@willsmanley You not getting it I have that setup already..! CSRF on its own mean nothing without cookies CSRF tokens are not JWT's. say a site loads on localhost:8000 and websockets load on port 6001 if you have strict cookie policies all requests to port 6001 will not get the path cookies on port 8000. Yes laravel validates subscriptions with Echo but only in the case of Pusher as far as i know. what i see here with socket.io is. the request is made by the echo server to laravel to authenticate the subscription. |
Thats why every subscription with request payload is sent with the CSRF token over websockets to the socket.io server then the server tries to authenticate the subscription. So instead of the browsers Echo client sending the request to authenticate a channel the http request params are sent over the websocket connection so the server can do that for the Echo client and authenticate all in one go. |
Sending a JWT token header to be sent to the socket.io server and changing the Broadcast routes guard to passport's api works fine. no errors. But sending JWT tokens for someother service to Act As is kinda mehhh. |
Oh right, well still inspect to see if the echo request has the session cookie attached. If it does, then it’s a problem with routing on port 6001. You can also inject a Log::debug() into the route for /broadcasting/auth route to inspect the request to see what cookies it has once it’s received by your app. |
I finally got pragmatic headers to work, Since I built a full Same Domain SPA the CSRF token is pragmatically stored in a state that is set via an None of the solutions above worked for setting the token AFTER the instance is crated, here is a solution that does:
The problem is if you login a user via ajax and do not reload the page then the token from the My use case looks like this:
|
For me the issue was that i had the echo server running on a different subdomain than the main application. App accessible under |
I get the above error when I run
laravel-echo-server start
. None of the issues I've searched in the issue tracker have helped so far. The event is also not being broadcasted. Queue and Broadcast drivers have been set to Redis. Broadcast service provider was correctly registered inapp.php
.I'm just using a default laravel app installation btw.
Full log: https://paste.laravel.io/bdcbf334-b7ce-46d5-b0a8-0b49703cc546#1,21
JS code:
Channels:
Event:
I call the event with:
Really at a loss here. No idea why I get the error after starting the web server or why I can't broadcast events.
The text was updated successfully, but these errors were encountered: