2017-09-15 52 views
0

儘管使用理論上有效的認證令牌,來自UserProvider的userQuery請求返回空user和networkStatus爲7,表明它已完成加載並且沒有「錯誤, 「儘管用戶爲空。GraphQL graph.cool用戶查詢不與Auth0令牌一起工作

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

回答

0

user查詢將在您的情況返回null,當在你的Graphcool項目中沒有User節點,在auth0UserId對應嵌入在JWT的Auth0 ID。

請將您的令牌粘貼到https://jwt.io/並檢查用戶是否存在帶有在其中打印的ID。 Auth0 ID以auth提供商開始,例如google-oauth2|<google id>