getSession always returns null on server #1466
-
Hey community! I'm trying to get the user's session on server side in order to render the correct page (login or dashboard) from the server. I'm using this on export const getServerSideProps = async ({ req }: NextPageContext) => {
const session = await getSession({ req })
console.log('SSR Session', session)
return {
props: { // doesn't even matter }
}
} but I'm also getting the following error in the console [next-auth][error][client_fetch_error]
https://next-auth.js.org/errors#client_fetch_error session FetchError: request to http://localhost:3000/api/auth/session failed, reason: connect ECONNREFUSED ::1:3000 If I replay the same GET request on the browser it works fine (and I'm getting the correct session as a response). I also tried to add a dumb endpoint I have Am I missing something super obvious here? Edit: I'm using |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 13 replies
-
could you please provide a bit more information about your configuration and how you use signin/signout? |
Beta Was this translation helpful? Give feedback.
-
Things that may cause this include:
The only other edge cases for this sort of thing I can think of are weird local DNS resolution problems caused by different resolvers for localhost in different contexts, but if that is the case then probably more thing are broken (so seems unlikely). If neither 1 or 2 apply to you could try |
Beta Was this translation helpful? Give feedback.
-
This solves it https://next-auth.js.org/configuration/nextjs#in-api-routes import { authOptions } from 'pages/api/auth/[...nextauth]'
import { getServerSession } from "next-auth/next"
export async function handler(req, res) {
const session = await getServerSession(req, res, authOptions)
... |
Beta Was this translation helpful? Give feedback.
-
If you are not able to get session use gettoken instead
Keys and URL
this is api route for calling token
and the in your layout/page
|
Beta Was this translation helpful? Give feedback.
-
Hi, everyone. I thought, it was the issue of Prisma but it is an issue with getSession. |
Beta Was this translation helpful? Give feedback.
-
For anyone seeing this issue with Next.js, you should use |
Beta Was this translation helpful? Give feedback.
-
create file: ' .eslintrc.json ' in the root of the application with the following contents:
|
Beta Was this translation helpful? Give feedback.
-
Using |
Beta Was this translation helpful? Give feedback.
Things that may cause this include:
Locally running firewall software that is preventing the server side route form connecting back to localhost (e.g. Windows Defender).
In this case you should be able to address this in the firewall settings.
Running inside a virtual machine / container where "localhost" is relative to the container.
There is no quick solution for this one yet (but one is in progress), but if it does apply to you there are some old posts about how work around this.
The only other edge cases for this sort of thing I can think of are weird local DNS resolution problems caused by different resolvers for localhost in different contexts, but if that is the case then pro…