nicolaslopezj / meteor-apollo-accounts

Meteor accounts in GraphQL
MIT License
146 stars 37 forks source link

Accessing userId in graphql server context #78

Closed matteodem closed 6 years ago

matteodem commented 6 years ago

I'm having some problems with accessing the userId on the meteor graphql endpoint. I'm setting the token with an ApolloLink

import { ApolloLink } from 'apollo-client-preset'

export const authMiddleware = new ApolloLink((operation, forward) => {
  operation.setContext(({ headers = {} }) => ({
    headers: {
      ...headers,
      authorization: localStorage.getItem('Meteor.loginToken') || null,
    }
  }))

  return forward(operation)
})

Another problem is that I cannot use the getLoginToken method in here as I don't see any example of async middleware. I'll create a separate ticket for that in the apollo-client repo though.

So it successfully sets the authorization header, but on the server side the context doesn't set the userId. Is some other code needed to have that working? Or is there another way to access the userId on the server?

matteodem commented 6 years ago

I've fixed this by using meteor-login-token as the key, instead of authorization, after checking the source code for the meteor apollo package: https://github.com/apollographql/meteor-integration/blob/master/src/main-server.js#L84