'cant read cookie in nextjs ssr

I have a problem with read a cookie in my browser.. here's my code

const dataA = authService.isLogin();

useEffect(() => {
  if (dataA) {
     router.push("/");
  }
}, [dataA]); ```

the dataA is get a cookie value from my browser.. but thats not working.. it caused my router.push not redirect to the page

how can i solve it ?


Solution 1:[1]

You should move dataA into useEffect hook because dataA run in SSR not browser so that can't read cookie from browser. Try like this:

useEffect(() => {
  const dataA = authService.isLogin();
  if (dataA) {
     router.push("/");
  }
}, [dataA]); 

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Toan Le