-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
It is not possible to pass Authorization header to WebSocket #3967
Comments
@michal-hudy Does this not work for you? const wsLink = new WebSocketLink(
new SubscriptionClient(WS_URL, {
reconnect: true,
timeout: 30000,
connectionParams: {
headers: {
Authorization: "Bearer xxxxx"
}
}
})
); |
It won't work as WebSocket API in browsers doesn't support setting custom headers, apart from the value of |
Please see @pkosiec comment. |
Struggled with adding async function for connection params, was getting
Just in case someone having the same issue. |
@pyankoff how can I read this header on server side? |
@georgyfarniev I think it depends on your server side implementation. I was using Hasura and didn't have to handle it. |
@pyankoff I'm using Hasura as well. how did you handle token change (refresh after expiration) with WebSocketLink? |
Hey, @pyankoff can you please share how you were able to re-authenticate the user once the token has expired. Thanks. |
@dreamer01 If i understand currently your question is like mine. |
Hey, @lroy83 this what exactly I meant, thank you for sharing the code. |
@lroy83 Oh man, I spent a long time searching for an example like what you posted. Thank you for sharing! |
Finally an actual code example for how to refresh tokens with websocket properly! Pasting it here just because the past 2 examples I've been linked 404'd.
It took literally forever to find your comment with this example. |
The code example by @pyankoff did not work for me. Instead, I had to supply the auth token in
|
@ARMATAV I've tried to implement code as mentioned, but it gives me type error for some of the wsClient functions. For example, when I try to call |
If I recall, yes - |
For me I needed to pass the authentication information on every request in a
According to the MDN docs, "The WebSocket.protocol read-only property returns the name of the sub-protocol the server selected;". Your server implementation will likely have a check for this to ensure protocol compatibility. Good news is we can pass many protocols. From the
We can pass the protools synchronously by instantiating the
However I wanted to be able to asynchronously pass in the authentication information once it changes. The class property
To asynchronously send the WebSocket Authentication information, we need to extend the
We can then create a WebSocket client and link as follows:
Somewhere else in your code, e.g after authentication you can update the authentication information as follows:
On logout user you can do
Note: This fix works by restarting the WebSocket client each time you set a new authentication information, it relies on the The server will then have access to the authentication information in its
Hope this helps someone. |
this work for me =>
|
Intended outcome:
Since Apollo Client 2 it is not possible to pass custom HTTP Header to WebSocket connection. In Apollo Client 1 it was possible by
Middleware
, but since version 2 it is not. I tried with additional linkconcat
, or by applyingMiddleware
tosubscriptionClient
.Sample with authorized link:
Sample with
subscriptionClient Middleware
Versions
The text was updated successfully, but these errors were encountered: