1

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

n9p4
  • 304
  • 8
  • 34
  • If the `API_URL` is a different host than the one that the frontend app is running, then you have CORS issues, and you need to set `withCredentials:true` to axios config. – Ivan V. Mar 25 '22 at 12:04
  • @IvanV. I tried it but still do not work, the API URL is the same. The only solution I come up is by doing: const cookie = { headers: { cookie: req.headers["cookie"], }, }; const resul = await axios.get(`${process.env.API_URL}/company/jobs`, { cookie, }); – n9p4 Mar 25 '22 at 12:21
  • Does this answer your question: [Why are cookies not sent to the server via getServerSideProps in Next.js?](https://stackoverflow.com/questions/69057271/why-are-cookies-not-sent-to-the-server-via-getserversideprops-in-next-js)? You need to explicitly pass the cookies to the axios request when making the request from the server-side. – juliomalves Mar 26 '22 at 14:33
  • Also, if that's an internal Next.js API route then you shouldn't be making a request to it inside `getServerSideProps`. You should move the logic that's in your API route directly to `getServerSideProps` instead. See [Internal API fetch with getServerSideProps? (Next.js)](https://stackoverflow.com/questions/65752932/internal-api-fetch-with-getserversideprops-next-js). – juliomalves Mar 26 '22 at 14:38

1 Answers1

0

According to documentation, you should pass to getSession function whole original req in API. and whole context prop in getServerSideProps

https://next-auth.js.org/getting-started/client#getsession

Wraithy
  • 1,722
  • 1
  • 5
  • 13