GraphQL graph.cool user query not working with Auth0 token

187 views Asked by At

Despite using a theoretically working authentication token, the userQuery request from UserProvider returns a null user and networkStatus of 7, indicating that it is done loading and that there is no "error," despite the user being null.

class UserProvider extends React.Component {
  render() {
    if (this.props.data.user) {
      window.localStorage.setItem("userId", this.props.data.user.id)
    }

    if (this.props.data.loading) {
      return <div>Loading</div>
    }

    return this.props.children(this.props.data.user)
  }
}

const userQuery = gql`
  query {
    user {
      id
    }
  }
`

export default graphql(userQuery, {
  options: { fetchPolicy: "network-only" },
})(UserProvider)

I am setting up the network interface with this code.

const wsClient = new SubscriptionClient(
  `wss://subscriptions.graph.cool/v1/redacted`,
  {
    reconnect: true,
  }
)

const networkInterface = createNetworkInterface({
  uri: "https://api.graph.cool/simple/v1/redacted",
})

const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
  networkInterface,
  wsClient
)

networkInterfaceWithSubscriptions.use([
  {
    applyMiddleware(req, next) {
      if (!req.options.headers) {
        req.options.headers = {}
      }

      console.log("applying middleware")

      // get the authentication token from local storage if it exists
      if (localStorage.getItem("auth0IdToken")) {
        console.log("apply header", localStorage.getItem("auth0IdToken"))
        req.options.headers["Authorization"] = `Bearer ${localStorage.getItem(
          "auth0IdToken"
        )}`
      }

      next()
    },
  },
])

let client = new ApolloClient({
  networkInterface: networkInterfaceWithSubscriptions,
})
1

There are 1 answers

0
marktani On

The user query will return null in your situation, when there is no User node in your Graphcool project where auth0UserId corresponds with the Auth0 id embedded in the JWT.

Please paste your token to https://jwt.io/ and check if a user exists with an id that is printed there. The Auth0 id starts with the auth provider, for example google-oauth2|<google id> for Google.