0

On my NextJS site I created authentication cookies to have the data on any page.

my code :

  res.setHeader("Set-Cookie", [
    serialize("access_token", access_token, {
      httpOnly: true,
      secure: process.env.NODE_ENV !== "development",
      sameSite: "lax",
      path: "/",
    }),
    serialize("token_type", token_type, {
      httpOnly: true,
      secure: process.env.NODE_ENV !== "development",
      sameSite: "lax",
      path: "/",
    }),
  ]);

However, I would like, in another page, to retrieve cookie data (read cookies) to display them on my page.

How I can do that ?

doctor pok
  • 13
  • 5
  • you can keep this data in localstorage also. plus you look answer here for react reading cooking https://stackoverflow.com/questions/57479576/how-to-read-cookies-with-react – Toufiq Ahmed Aug 15 '21 at 15:19
  • when I try to do `cookies.setItem ("API_TOKEN", "hello", undefined, "/", undefined, "https");` it tells me that `document is not defined` pointing to the module – doctor pok Aug 15 '21 at 15:41
  • And if not, would you have another solution? – doctor pok Aug 15 '21 at 15:43
  • Move your cookies access inside a `useEffect` on the client-side code. Also, bear in mind that setting [`httpOnly: true`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on the cookie will make it inaccessible to JavaScript code on the client. – juliomalves Aug 15 '21 at 16:38
  • ok but after the `useEffect` how do I get the value because? Do you have an example? – doctor pok Aug 15 '21 at 17:54

0 Answers0