getSession() not working in a graphql endpoint with apollo-server-micro #3803
-
Question 💬Hi, I'm setting up a project with next.js next-auth and graphql, but next-auth can't authenticate requests with getSession to the graphql endpoint. It works fine everywhere else, and the problem only appears when deployed to vercel, not when developing locally. I presume this has to do with how micro handles requests, and that the interface is different. What can I do to get it working. Can I pass the token manually? I did not find a function that authenticated a token manually.
How to reproduce ☕️You can clone the repo at https://github.com/JulianBuse/todo. There are a few environment variables that need to be filled out in .env: this will work in local development(at least it does for me), but when I deploy it to vercel, the /api/graphql endpoint does not recognize that I am logged in. I am developing on an m1 pro macbook. Contributing 🙌🏽Yes, I am willing to help answer this question in a PR |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 14 replies
-
Facing the same issue,getSession is always null in apollo context for loggedin user. |
Beta Was this translation helpful? Give feedback.
-
This isn't a const apolloServer = new ApolloServer({
typeDefs,
resolvers,
plugins: [
ApolloServerPluginLandingPageGraphQLPlayground({
settings: {
"request.credentials": "include",
},
}),
],
context: async ({ req, res }) => {
const session = await getSession({ req });
return { session };
},
}); |
Beta Was this translation helpful? Give feedback.
-
Update to node v18 |
Beta Was this translation helpful? Give feedback.
-
I was trying to get the session information of the currently logged in user in my
I hope this helps everyone who is trying to get the session information in apollo server context while using next-auth for authentication purposes. |
Beta Was this translation helpful? Give feedback.
This isn't a
next-auth
problem. This is related to the Apollo GraphQL Playground. The playground does not send credentials unless explicitly stated with"request.credentials": "include"
.