Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo Sessions and Cookies Authenticating the Username and Password

Anand Mohan Duddella
PLUS
Anand Mohan Duddella
Courses Plus Student 8,264 Points

the session id which is set with the user id in the User.authenticate() function is not available outside

I have assigend similarly req.session.userId = user._id but when i console.log this information in the current route i can see that the information is added to the sessions object but when i am out of this routes to the index route for example and console.log the information I can not see the userid stored in the callback function of User.authenticate

2 Answers

Anand Mohan Duddella
PLUS
Anand Mohan Duddella
Courses Plus Student 8,264 Points

I have tried assigning the value but it did not work with the global variable but serialising the user at the time of login and deserialising by using PassportJs node module worked for me.

Ryan Zimmerman
Ryan Zimmerman
3,854 Points

You are in the Global scope and the value was assigned in another scope. req.session has ended it's scope.

Try assigning the value to a global variable to see if you can access it after the call back has invoked.