I have Apollo server mutation for my login:
// resolvers.js
loginUser: async (root, args, context) => {
const currentUser = await prisma.user.findUnique({
where: {email: String(args.email)},
});
if (currentUser) {
const token = jwt.sign(currentUser, 'supersecret');
console.log('resolvers.js :', token);
context.cookies.set('auth-token', token, {
httpOnly: true,
sameSite: 'lax',
maxAge: 6 * 6 * 60,
});
}
return currentUser;
},
This works. It returns the currentUser
and it places a JWT in the cookies:
index.js: undefined
resolvers.js : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJwZXRlckBjbGVhbnBpeGVsLm5sIiwibmFtZSI6IlBldGVyIEJvb21zbWEiLCJwYXNzd29yZCI6IiQyYiQwNCREcW80TE4zUzVaT25rMVQxTXlhdE91aWpidkhwbDI4MW9EQ2hIZEV1MVpmRU95azdtakxxdSIsImlhdCI6MTYzMDU4MTgwNX0.GDbhFDVaVQaZVRZc7w7XUrCNffy2D0XTunEQ95Tl***
But in my Apollo server initialization I can't get the cookie:
// index.js
const server = new ApolloServer({resolvers, typeDefs,
context: ({req, res}) => {
prisma;
const cookies = new Cookies(req, res);
const token = cookies.get('auth-token');
console.log('index.js: ', token);
const user = verifyToken(token);
return {
cookies,
user,
};
},
});
When I do do something, for example add a movie to my database I get an:
index.js: undefined