getCookie(“cookiename”) is showing value with % how to get the value without it

I’m setting a cookie in my node server code using the following:

let userid = db._id  //console.log shows the id as :  abc123456789d
res.cookie("uid", userid , { sameSite: "none", secure: true })

Is this the right way or am I doing anything wrong?

in my front application I use js to get the cookie using the following code :

let x =  document.cookie
  let cookie = getCookie("uid")
  console.log("cookies value:",cookie)

the console log shows the cookie value as this :

j%3A%abc123456789d%22

I tried to set the cookie with JSON.stringify() and the front is showing the cookie value as this:

%3A%abc123456789d%22

My question is why I don’t get the value without the % value %22 and how to correct this output so I get the value of the id as id that I get from the db which should be

abc123456789d

Note that the id I mentioned here is just for demonstration purpose only and doesn’t exist in real life. The id I get back from my db is 48 character with just letters and numbers (no special character, something like 666de2f0600eda239ae05d88)

It’s my first time I try to get a cookie value using js so it seems that I’m missing something and after reading online, I can’t figure out why this % %22 surround my id. Any idea why and how to fix it?

Answer

my guess is missing URI decode/encode

The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent