1

I am building a simple web app using nodejs as backend with angularjs front-end.

With nodejs I am setting some cookies after a user logs in.

res.cookie(name,value)

I am creating two cookies each for username and user_id. and when I see the set cookies, using

res.json(req.cookies)

and the json response have the actual user_id and also with a another key connect.sid, maybe because I am using connect-flash, I don't know.

But when I access the cookies in angularjs using $cookies.get('user_id') the value is some encoded value,

For eg : for actual user_id : 571be8d34666cf69664645d5,

I am getting j%3A%22571be8d34666cf69664645d5%22

How can I get the actual userid ?

I know that the real ID is in between the string and I can get it by using some string manipulation, but is there another way.

I am using passportjs, express-session, connect-flash, connect-mongo,cookie-parser in my app.

2
  • where does the value come from? Is it an object? Commented Apr 23, 2016 at 23:55
  • I am setting the value using req.user._id Commented Apr 24, 2016 at 0:20

2 Answers 2

1

Instead of using two different cookies, you can set the object in the cookie from express, and on angular side you can get the cookies by:

$cookies.getObject('cookiename',object)

In your case either write a function for splitting the cookie or use JSON.parse(json) for parsing the cookie coming from express. I did it other way round and managed to read the cookie object in express by JSON.parse(cookieobj.user_id)

Sign up to request clarification or add additional context in comments.

Comments

0

It seems that as of angular 1.4, you should use $cookies.getObject('user_id') for deserialize version of the value. Start with that, if not a solution, check on the console the actual value of the cookie to try to understand where the j:"571be8d34666cf69664645d5" come from.

1 Comment

I looked in the angular docs and tried that already, its giving me an error : Unexpected token j in JSON at position 0. On the console, document.cookie is also giving me the same value. And I can't understand where is this coming from, as the username cookie is intact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.