【问题标题】:graphql apollo-datasource-rest pass jwt tokens from frontend to authenticate custom REST APIsgraphql apollo-datasource-rest 从前端传递 jwt 令牌以验证自定义 REST API
【发布时间】:2019-11-05 08:56:56
【问题描述】:

如何将 JWT 令牌从前端传递到 Apollo graphql 服务器并将其转发以调用不同服务器上的自定义数据源 REST API 以提取每个登录用户的个人资料信息?

【问题讨论】:

    标签: node.js graphql datasource apollo-server


    【解决方案1】:

    解决了,不知道很简单。

    const server = new ApolloServer({
            schema,
            dataSources,
            context: ({ req }) => {
                let authToken = null;
                try {
                    authToken = req.headers[AUTH_TOKEN];
    
                    if (!authToken) {
                         console.error(`No ${AAUTH_TOKENU} header present.`)
                    }
                 } catch (e) {
                    console.warn(`Unable to authenticate using auth token: ${authToken}`);
                 }
                 console.log(authToken)
                 return {
                     db, authToken
                 }
            } 
        });
    

    您可以在解析器定义中传递令牌。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-25
      • 2021-09-04
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 2016-01-01
      相关资源
      最近更新 更多