getSession({req}) not working in api route #4647
Replies: 12 comments 8 replies
-
i hope you are not accessing the api using postman or something related ? |
Beta Was this translation helpful? Give feedback.
-
@Klin-Coders Nope, just a plain ol' fetch in getServerSideProps of one of my pages |
Beta Was this translation helpful? Give feedback.
-
firstly its is not advised to call your api routes using getServerSideProps... but do you mind showing how you are using it in getServerSideProps?? |
Beta Was this translation helpful? Give feedback.
-
@Klin-Coders
As far as I can see, this is not discouraged, it is a valid use case and NextJS nor Next Auth mention anything to suggest you cannot / should not do data fetching in getServerSideProps. This is my function export async function getServerSideProps(ctx){
let session = await getSession(ctx);
console.log("Session", session); // Not null here
let response = await fetch(process.env.NEXT_PUBLIC_HOST+`/api/orders/getAll?page=${ctx.query.page}`);
if(response.status == 200){
var blob = await response.blob();
var json = await blob.text();
var responseJson = JSON.parse(json);
return { props: { data: responseJson, session: session } };
}else{
return { props: { data: {}, session: session } };
}
} |
Beta Was this translation helpful? Give feedback.
-
I do not see a reason why it should be null, this seems clean but then here is the link to the NextJS docs that explains why you should not call your api routes from getServerSideProps or API Routes It can be tempting to reach for an API Route when you want to fetch data from the server, then call that API route from getServerSideProps. This is an unnecessary and inefficient approach, as it will cause an extra request to be made due to both getServerSideProps and API Routes running on the server. Take the following example. An API route is used to fetch some data from a CMS. That API route is then called directly from getServerSideProps. This produces an additional call, reducing performance. Instead, directly import the logic used inside your API Route into getServerSideProps. This could mean calling a CMS, database, or other API directly from inside getServerSideProps. |
Beta Was this translation helpful? Give feedback.
-
@Klin-Coders That is actually helpful, perhaps next-auth just doesn't support getSession() when calling the api route from getServerSideProps. I have instead moved my CRUD code to a service, for which I can call in getServerSideProps and when I need to make client side calls (useEffect) I still hit an API endpoint, but just re-use the service I wrote. Thank you! It would still be good to get an answer however, on whether the initial scenario i was trying should be possible. |
Beta Was this translation helpful? Give feedback.
-
yes we wait, maybe others will have a better insight |
Beta Was this translation helpful? Give feedback.
-
This should work as expected. See: https://next-auth-example.vercel.app/api-example https://github.com/nextauthjs/next-auth-example/blob/main/pages/api/examples/session.ts @Klin-Coders is correct though #4620 (comment) We are working on a better API that does not rely on There is a currently undocumented |
Beta Was this translation helpful? Give feedback.
-
@balazsorban44 I am not seeing anywhere in the example where getServerSideProps is making a call to a protected api endpoint with a getSession() call in it, am I missing something? I think that exact use case is where it is failing. I did give the getServerSession a try, but that also did not work. Thank you |
Beta Was this translation helpful? Give feedback.
-
The initial description/issue title did not mention A reproduction repository would be very heplful. |
Beta Was this translation helpful? Give feedback.
-
Similar issue here with |
Beta Was this translation helpful? Give feedback.
-
For the record, I had a similar scenario. I upgraded from I replaced all @balazsorban44 I'm not sure if this can be considered a regression or this is as-it-should-work, the documentation hints that it's better to use |
Beta Was this translation helpful? Give feedback.
-
Question 💬
Hello!
I have an api route which I am trying to protect by getting the session using
const session = await getSession({req})
.However, it always returns
null
; Any help diagnosing the issue here is much appreciated, I have looked through the example app and I really can't see what I am doing differently that would cause this.How to reproduce ☕️
My api route is as follows:
[...nextauth].js
_app.js
It is worth noting that the sign in / sign out works correctly and shows my email when I am logged in at the same time that getSession() in my api route returns null
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
Beta Was this translation helpful? Give feedback.
All reactions