Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 195 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set cookie in express.js appear j: prefix

#1
I'm trying to set cookie using res.cookie like below:

res.cookie('userId',req.user._id); //set cookie here
console.log(req.user._id); //returned correct value, eg abc

then I'm seeing j:"abc" in my cookie, why does this happens?
Reply

#2
Cookies are encrypted to the client side. You need a [cookie-parser][1]
to correctly get the user.id from your cookie. See its documentation for use.

[1]:

[To see links please register here]

Reply

#3
According the the [Express 4 docs](

[To see links please register here]

), `res.cookie(name, value [, options])` sets a cookie name to a value. The value parameter may be a string or object converted to JSON.

In this instance, `req.user._id` is an object so you would set the cookie as `res.cookie('userId', JSON.stringify(req.user._id))`
Reply

#4
I know this is a bit late, but I came across this issue myself and have been digging around a bit. It seems they're prefixing any JSON strings with "j:" so they know it's a JSON string when parsing it back. What this basically means is that you have to manually remove the "j:" if you're using some other way of parsing it.
Reply

#5
So I'm using cookie-parser & express-session on the NodeJS side, ng2-cookies on the client side. I was also expecting to read userId as i.e. `59bca61b74d1cac10ce50d0c` rather than `j:59bca61b74d1cac10ce50d0c` :(

So, rather than have to do some magic on the client side I just did `res.cookie('cookieName', cookieValue.toString(), cookieOptions)` and this gave me what I was looking for.

Doing a `console.log('cookies', req.cookies)` shows things are fine, although req.headers.cookie shows 2 userId cookies (still testing)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through