GraphQL graph.cool用户查询不使用Auth0令牌

时间:2017-09-15 21:29:59

标签: reactjs graphql auth0 graphcool

尽管使用了理论上有效的身份验证令牌,但UserProvider的userQuery请求返回null user和networkStatus为7,表示已完成加载并且没有"错误,"尽管用户为空。

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)

我正在使用此代码设置网络接口。

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 个答案:

答案 0 :(得分:0)

当您的Graphcool项目中没有user节点null与JWT中嵌入的Auth0 ID对应时,User查询将在您的情况下返回auth0UserId

请将您的令牌粘贴到https://jwt.io/并检查用户是否存在其中已打印的ID。 Auth0 id以身份验证提供程序开头,例如Google的google-oauth2|<google id>