【问题标题】:GraphQL graph.cool user query not working with Auth0 tokenGraphQL graph.cool 用户查询不适用于 Auth0 令牌
【发布时间】:2018-02-25 03:02:29
【问题描述】:

尽管使用了理论上有效的身份验证令牌,但来自 UserProvider 的 userQuery 请求返回 null user 和 networkStatus 7,表明它已完成加载并且没有“错误”,尽管用户为 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)

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

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

【问题讨论】:

    标签: reactjs graphql auth0 graphcool


    【解决方案1】:

    user 查询将在您的情况下返回 null,如果您的 Graphcool 项目中没有 User 节点,其中 auth0UserId 对应于嵌入在 JWT 中的 Auth0 id。

    请将您的令牌粘贴到https://jwt.io/ 并检查是否存在具有打印在那里的 id 的用户。 Auth0 id 以身份验证提供程序开头,例如 Google 的 google-oauth2|&lt;google id&gt;。

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 2018-08-04
      • 2021-04-08
      • 2020-03-31
      • 2017-10-18
      • 2019-12-27
      • 2017-10-31
      • 2021-08-14
      相关资源
      最近更新 更多