0

im just trying to look for an explanation here. so i have a front end using Next and backend with Express and i want them to exchange cookies. ive read several topics regarding these and found 2 supposedly working methods, however only 2 of the methods worked for me.

these are the settings for Express (note i use the library cookie-parser)

app.use(cors( {origin: 'http://localhost:3000',credentials: true}))

and now the issue comes with attaching cookies in my requests from Next with Axios. the working solution for me that i got was attaching the cookies manually in the request like so.

export async function getServerSideProps(context) {
    const res = await axios.get("/contact/get-all",{ withCredentials: true, headers: {
      Cookie: context.req.headers.cookie
   }, });
    const data = await res.data
    // const data = context.req.headers



    // Pass data to the page via props
    return { props: { data } }
  }

now another solution that people said to have worked is to include credentials like

withCredentials: true

or

axios.defaults.withCredentials = true

but in my express when i try to get the cookies with this method

req.cookies

it returns null. why is this? am i missing something?

kevin
  • 29
  • 1
  • 7
  • You have to explicitly pass the cookies to the axios request because `getServerSideProps` runs on the server and cookies are not automatically sent like in a browser environment. See [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). – juliomalves Dec 11 '22 at 16:47

0 Answers0