Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a cookie in the req.session in node express?

It's more for my understanding. Why is there a cookie object in the req.session in express? I use req.session to store the login status and username. The cookie object in req.session is the same as the cookie properties in the client cookie, placed by express.parseCookie but without sessionID. Can anyone explain why this is in there? It cant be for the purpose to identify cookie and session because thats already made by the cookie value and the session ID (req.session.ID), or am I'm wrong?

- req.session --> 
{ cookie: 
{ path: '/',
 _expires: null,
 originalMaxAge: null,
 httpOnly: true },
  loggedIn: true,
  username: 'luidpold' }
like image 877
marschro Avatar asked Oct 21 '22 18:10

marschro


1 Answers

For convenience. It’s the cookie associated with the session, and you can query/modify it.

The session handler in Express comes from Connect. The documentation for Connect says:

Session#cookie

Each session has a unique cookie object accompany it. This allows you to alter the session cookie per visitor. For example we can set req.session.cookie.expires to false to enable the cookie to remain for only the duration of the user-agent.

Session#maxAge

Alternatively req.session.cookie.maxAge will return the time remaining in milliseconds, which we may also re-assign a new value to adjust the .expires property appropriately.

like image 70
Nate Avatar answered Oct 23 '22 22:10

Nate