简体   繁体   English

Apollo-server password.js 认证

[英]Apollo-server password.js authentication

我是 node.js GraphQL apollo-server 的新手,我第一次使用 password.js。我需要使用passport.js.my token 进行身份验证,如果border 存储在cooks 中。我被折磨了7 天做这个。请,谁能向我解释我应该如何使用 Apollo-server password.js 做这一切?

Your question is unanswered within the community as there is so many ways to do authentication with graphQL.您的问题在社区内没有答案,因为使用 graphQL 进行身份验证的方法有很多。 Here is a 1 arbitrary example to get you on your way.这是一个让您上路的任意示例。

Context - The function is executed on each request to your graphQL server.上下文- 该函数在对你的 graphQL 服务器的每个请求上执行。

const server = new ApolloServer({
    context: ({ req }) => {
        try {
            const authToken = req.header["Auth-token"];

            const user = passport.authenticate("local", authToken);

            return { user };
        } catch (error) {
            console.error(error);

            return {};
        }
    }
}); 

Whatever is returned from the Context function is available inside your resolvers as the third argument.Context函数返回的任何内容都可以在解析器中作为第三个参数使用。

const resolvers = {
    Query: {
        getUserCheese: (root, args, context) => {
            return findCheeseByUserName(context.user.name);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM