0

I use reactjs and nodejs. I create an api url which create my jsonwebtoken

const createToken = (id) =>{
  return jwt.sign({id},123)
}

module.exports.mandatoryTokens = async (req,res) =>{
  try{
    const token = createToken(new Date())
    res.cookie('force',token, {httpOnly : true})
    res.status(200).send('ok')
  }
  catch(err){
    res.status(200).send(err)
  }
}

this is my reactjs side

const App = () => {

  const handleAcceptMandatoryCookie = () =>{
    axios({
      method : "get",
      withCredentials : true,
      url : `${process.env.REACT_APP_API_URL}api/token`
    }).then((res)=>{
      console.log(res)
    }).catch((err)=>{
      console.log(err)
    })
  }
  return (
    <div>
      <CookieConsent debug={true} cookieName="force" buttonText="Accept" onAccept={handleAcceptMandatoryCookie}>
        click on accept
      </CookieConsent>
    </div>
  )
}

My issue is even if I click on accept it keeps show me the cookie consent bar to accept the cookie again. How can I keep it hiding if there is the cookie in my cookies list

user10863293
  • 770
  • 4
  • 11
  • 32

2 Answers2

0

Think about it like this: How do you show the <CookieConsent /> component only when accepted === false?

First, add a state to your component to handle the status of 'accepted'.

const [isAccepted, setIsAccepted = useState(false)

Secondly, only show your component when isAccepted is false:

!isAccepted && <CookieConsent .../>

Thirdly, toggle the isAccepted inside of your handleAcceptMandatoryCookie function:

setIsAccepted(true)
P Savva
  • 309
  • 1
  • 5
  • The issue is with react-cookie-consent the bar shouldn't be visible when the cookie exist because when I try without the server side react-cookie-consent create a cookie and when the cookie exist it doesn't show the bar but when I create it with my server side it doesn't work – user10863293 May 03 '22 at 13:21
0

Remove debug={true} of <CookieConsent /> props.

From the docs.

md10
  • 1
  • 1