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

Authenticated subscriptions #343

Closed
Weakky opened this issue May 28, 2018 · 16 comments
Closed

Authenticated subscriptions #343

Weakky opened this issue May 28, 2018 · 16 comments

Comments

@Weakky
Copy link

Weakky commented May 28, 2018

Hey there,

I'm trying to setup authenticated subscriptions.

As this issue suggests (apollographql/subscriptions-transport-ws#113) it should be possible using a middleware-like approach using the following code:

const wsLink = new WebSocketLink({
  uri: wsUri,
  options: {
    reconnect: true,
  },
});
wsLink.subscriptionClient.use([{
  async applyMiddleware(options, next) {
    const token = await getLoginToken();
    options.context = { token };
    next();
  },
}]);

Unfortunately, I'm receiving a context that is always null. However, the context is properly set when instantiating the WebSocketLink with options.connectionParams.

As I don't know whether the issue is coming from apollo-client, subscription-transport-ws, graphql-yoga, or just myself, I'm asking that question here.

Is there anything I'm doing wrong ?

Cheers!

@Weakky Weakky changed the title Authenticated subscription Authenticated subscriptions May 28, 2018
@0xDaksh
Copy link
Contributor

0xDaksh commented May 28, 2018

I did it like this:

const wsLink = new WebSocketLink({
  uri: 'ws://localhost:4000',
  options: {
    reconnect: true,
    connectionParams: {
      token: myToken,
    },
  },
});

You've to directly specify that in options, I guess. As that's how i did it, a while back.

@Weakky
Copy link
Author

Weakky commented May 28, 2018

Yeah, but that's what I said above:

However, the context is properly set when instantiating the WebSocketLink with options.connectionParams.

In most cases though, you don't have that token when instantiating the WebSocketLink.
That is something you have access to only after user has logged-in. That's why a middleware approach would be the right way to handle it.

@0xDaksh
Copy link
Contributor

0xDaksh commented May 28, 2018

what you can do in those cases, is that you can store the token into a cookie or localStorage and reload the page. when Websocket link is created, you can just ask it to read it from there.

@0xDaksh
Copy link
Contributor

0xDaksh commented May 28, 2018

Configure your websocket-link like this:

const wsLink = new WebSocketLink({
  uri: 'ws://localhost:4000',
  options: {
    reconnect: true,
    connectionParams: {
      token: localStorage.getItem('your-token-name') || null,
    },
  },
});

once you have token, send it to localstorage and reload:

// recieved token,
localStorage.setItem('your-token-name', 'tokenValue')
window.location.reload() // reload the page

In most cases, after login we make the browser reload, because it get's the login state and essence.

@0xDaksh
Copy link
Contributor

0xDaksh commented May 28, 2018

also try this:

wsLink.subscriptionClient.use([{
  async applyMiddleware(options, next) {
    const token = await getLoginToken();
    options.connectionParams = { token };
    next();
  },
}]);

@Weakky
Copy link
Author

Weakky commented May 28, 2018

While this could be a workaround for browsers, it cannot work on mobile apps. And sadly, that doesn't explain why the middleware approach isn't working :(

EDIT: I think I've tried that but I'm not sure, I'll try that again to make sure !

@0xDaksh
Copy link
Contributor

0xDaksh commented May 28, 2018

I think it's not working because, you're adding it as options.context, while it works in options.connectionParams.

Edit: nvm, connectionParams is used at initializing most probably

@0xDaksh
Copy link
Contributor

0xDaksh commented Jun 4, 2018

@schickling as there is no response on this issue, please close it. @Weakky can reopen it, if they still have any doubts.

@Weakky
Copy link
Author

Weakky commented Jun 4, 2018

This is still an issue. Using options.connectionParams in the middleware doesn't work. 😞

@0xDaksh
Copy link
Contributor

0xDaksh commented Jun 4, 2018

@Weakky can you confirm that this also doesn't work with apollo-server-express? so we can make sure, this is a graphql-yoga problem or not?

@kamilregulski
Copy link

@stale
Copy link

stale bot commented Nov 23, 2018

Due to inactivity of this issue we have marked it stale. It will be closed if no further activity occurs.

@stale stale bot added the stale label Nov 23, 2018
@stale
Copy link

stale bot commented Nov 30, 2018

Hey 👋, It seems like this issue has been inactive for some time. In need for maintaining clear overview of the issues concerning the latest version of graphql-yoga we'll close it.
Feel free to reopen it at any time if you believe we should futher discuss its content. 🙂

@stale stale bot closed this as completed Nov 30, 2018
@joffarex
Copy link

I would like to reopen this issue, as there is no clear way to authenticate subscriptions in graphql-yoga

@HugoLiconV
Copy link

I'm having the same issue, I'm using GraphQL Shield for permissions and I need to access the token in every operation in an express middleware, right now I'm able to access it through the context but only in the first connection, not in every operation

server.express.use(async (req, res, next) => {
  /*
     I need to access webSocket authToken here
  */
  next();
});

@Urigo
Copy link
Collaborator

Urigo commented Mar 29, 2022

Hey, @Urigo from The Guild here!

You might know us from projects such as graphql-code-generator, envelop or graphql-tools.

For a long time we thought that the Javascript ecosystem is still missing a lightweight cross-platform, but still highly customizable GraphQL Server.

In the past the awesome Prisma team took on that great challenge and now we are happy to announce that we are continuing them and just released GraphQL Yoga 2.0 - Build fast, extensible, and batteries-included (Subscriptions, Serverless, File uploads support) GraphQL APIs in Node.js 🚀

We have been working a long time on version 2.0 and have been using it in our clients projects for a few months now and shared a couple of alpha cycles here.
Thank you all for your feedback and suggestions, you made this release possible!

Please try Yoga out again, give us feedback and help us spread the word on the new release!

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

No branches or pull requests

6 participants