Skip to content
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

Open
zhigang1992 opened this issue Aug 13, 2020 · 5 comments
Open

Updating webSock token #5581

zhigang1992 opened this issue Aug 13, 2020 · 5 comments

Comments

@zhigang1992
Copy link

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's headers field

But 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.

@phamquyhai
Copy link

follow this

@zhigang1992
Copy link
Author

@phamquyhai sry I think you meant to post a link? but I don't see anything. :(

@wawhal
Copy link
Contributor

wawhal commented Aug 26, 2020

@zhigang1992 check if the solutions mentioned here work out for you: apollographql/subscriptions-transport-ws#171

@jbool24
Copy link

jbool24 commented Sep 22, 2020

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.

@jbool24
Copy link

jbool24 commented Sep 22, 2020

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
                })
          }
        };
      }
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants