-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Updating webSock token #5581
Comments
follow this |
@phamquyhai sry I think you meant to post a link? but I don't see anything. :( |
@zhigang1992 check if the solutions mentioned here work out for you: apollographql/subscriptions-transport-ws#171 |
Sorry, but all these solutions seem to use Sync methods of retrieving a token. Anyone using an Async method to apply a Bearer token to the WSS after it is received from a remote token provider? I feel like manual close/open of the socket after a token is received is the only way but I can't can yet grasp the timing of the ApolloClient's init process for websocket. Any suggestions would be much appreciated as I've already got gray hairs spending 3.5 days trying to find a solution. |
For posterity and anyone who may hit this in the future I finally found what worked below. Turns out async ConnectionParams are allowed, I simply returned a malformed object on previous attempts. Setting ONLY the header field on the params did work after all. const wssLink = new WebSocketLink({
uri: process.env.REACT_APP_GRAPHQL_WS_ENDPOINT,
options: {
reconnect: true,
lazy: true,
connectionParams: async () => {
const token = await getAccessTokenSilently({
audience: process.env.REACT_APP_API_AUDIENCE
});
if (typeof Storage !== "undefined" && token !== undefined) {
localStorage.setItem("token", token);
}
return {
headers: {
...(token
? {
Authorization: `Bearer ${token}`,
"x-hasura-role": "user"
}
: {
"x-hasura-admin-secret": process.env.REACT_APP_GRAPHQL_SECRET
})
}
};
}
}
}); |
If JWT token is set to expire at 15 mins,
And if WebSocket connected prior to expiration, it will keep using the one from
connection_init
'sheaders
fieldBut after a minute, token expires, sequential request will get token invalid error.
apollographql/apollo-link#197 (comment)
Is possible to add header to all the request from client. but hasura doesn't pick them us expect the one from connectionParams.
And there are other suggestions to close the connection, that also won't work for us since there might be ongoing subscription that need to watch for new result.
The text was updated successfully, but these errors were encountered: