'NextJS not persisting cookies from production server

I have a nextJS app with a node js frontend.

For authentication I use JWT token and then set it as a cookie.

Node JS Code:

res.cookie('token', token, {
    secure: true,

    // Same site is none because server and client have different urls
    sameSite: 'none',

    // Expiry 10 years
    expires: new Date(new Date().setFullYear(new Date().getFullYear() + 10)),
  })

And I just make a simple request from my NextJS App:

 const response: LoginResponse = await (
        await fetch(`${SERVER_URI}/login`, {
          method: 'POST',
          credentials: 'include',
          body: JSON.stringify(data),
          headers: { 'Content-Type': 'application/json' },
        })
      ).json()

Everything works fine and the cookies are set properly

enter image description here

But when I reload the page the cokkies get deleted and I lose all data.

When I run from my local server everything seems to work fine but the problem arises when I run the node server on production.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source