If API is accessed as GET
request from the browser as authenticated, you get the data.
But when you want to access the data by fetching in client-side by the function getserverside gives me the error that is not authenticated which it fact it is.
API
export default async (req, res) => {
const { method } = req;
const cookie = {
headers: {
cookie: req.headers["cookie"],
},
};
const session = await getSession({ req: cookie });
if (!session) {
return res.status(401).json({
success: false,
message: "NOT Authorized",
});
}
switch (method) {
case "GET":
try {
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,
});
}
Component
export async function getServerSideProps({ req, res }) {
console.log(req.cookies); // Logs all cookies from the request
const cookie = {
headers: {
cookie: req.headers["cookie"],
},
};
const session = await getSession({ req: cookie });
console.log(session); // session is received perfectly.
const res = await axios.get(`${process.env.API_URL}/company/jobs`); // ERROR NOT AUTHENTICATED
}
It is a bit weird because when I try to access the data the cookie is received but when I make the request it says is not authenticated