'getSession returning null in next js API only in Get requests
I am trying to make a request (logged in with session), to get the id from the session and to make a request using that ID. But the session is null, when I do a post request the session is okay.
Component
export async function getServerSideProps(context) {
const res = await axios.get(`${process.env.API_URL}/company/jobs`);
return {
props: {
jobs: res.data.jobs,
},
};
}
API Request
import { getSession } from "next-auth/react";
export default async (req, res) => {
const { method } = req;
switch (method) {
case "GET":
try {
const session = await getSession({ req });
console.log(session + " SESSION "); //RETURN NULL
console.log(JSON.stringify(session));
const jobs = await prisma.jobs.findMany({
where:{
userId: session.id
}
});
return res.status(200).json({ success: true, jobs });
} catch (error) {
return res.status(404).json({
success: false,
message: error.message,
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
