【发布时间】:2019-02-08 01:00:08
【问题描述】:
所以我有一个应用程序通过 Auth0 登录并将 jwt 令牌保存在 cookie 中。
我还有一个检索数据的 Apollo Server 2。如何保护 Apollo Server 并仅在用户登录并通过 Auth0 服务器验证时才返回数据?
下面的代码直接来自https://www.apollographql.com,但我不明白的是如何处理下面的getUser(token),以实际检查授权标头中的有效JWT,如果存在,用户将被允许访问受保护的资源?
// using apollo-server 2.x
const { ApolloServer } = require('apollo-server');
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => {
// get the user token from the headers
const token = req.headers.authorization || '';
// try to retrieve a user with the token
const user = getUser(token);
// add the user to the context
return { user };
},
});
server.listen().then(({ url }) => {
console.log(`???? Server ready at ${url}`)
});
【问题讨论】:
标签: node.js graphql auth0 apollo apollo-server