Edgar
Edgar

Reputation: 6858

Apollo-server password.js authentication

I'm new to node.js GraphQL apollo-server, and for the first time I use password.js.I need to do authentication using passport.js.my token should border is stored in cooks.I am tormented for 7 days to do this.please, who can explain to me how should I do all this using Apollo-server password.js ?

Upvotes: 0

Views: 165

Answers (1)

Dan Starns
Dan Starns

Reputation: 3815

Your question is unanswered within the community as there is so many ways to do authentication with 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.

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.

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

Upvotes: 2

Related Questions