Home > Software engineering >  NextJS access cookie response in React components
NextJS access cookie response in React components

Time:08-17

I'm using Next js along with Express backend, for authentication I,m using Cookie, and for protected routes, I have used Next js middleware which is fine, but now I want to access the cookie response in React components to check if we have a cookie then I want to hide register and login links from the navbar, please anyone can help me?

CodePudding user response:

You can use the js-cookie package to access cookies on the frontend.

Do an import in the react component first:

import Cookies from 'js-cookie'

Then access the cookie, maybe do it in useEffect to avoid any issues:

useEffect(()=>{
  const allCookies = Cookies.get();
  console.log(allCookies);
}, []);

CodePudding user response:

For client-side checking, you can use cookieCutter

import cookieCutter from 'cookie-cutter'
cookieCutter.get('myCookieName')
  • Related