question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide newly created session as an argument for regenerate callback

See original GitHub issue

The session regeneration is not happening in-place (which is not clear from documentation), but it’s done by reassigning the session prop on req object. In order to access regenerated session, user either has to always pass request object together with session object or he has to use session.req.session (weird and probably not part of a public API).

// somewhere outside of middleware, without an access to request
this.session.regenerate(error => {
  if (error) {
    return reject(error);
  }      
  this.session.userId = user.id; // OLD SESSION !!! won't work
  this.session.req.session.userId = user.id // works but wtf...    
  return resolve();
});

I think it would make sense to make new session object accessible via callback args:

// somewhere outside of middleware, without an access to request
this.session.regenerate((error, session) => {
  if (error) {
    return reject(error);
  }      
  this.session = session; // update session
  this.session.userId = user.id // store something    
  return resolve();
});

I also think it better indicates that the “old” session is obsolete…

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Jan 1, 2017

In addition, I feel like this issue is also saying that the way people will use this API is that they just won’t even read the README. I think the README (https://github.com/expressjs/session#sessionregeneratecallback) is very clear on exactly what this method does and where the regenerated session will become accessible from.

0reactions
urugatorcommented, Jan 1, 2017

Even though I have read the README, I have to admit I really missed the part:

instance will be initialized at req.session

my apologies for that (I was punished already, it took me some time to find the problem), however maybe the example which follows:

req.session.regenerate(function(err) {
  // will have a new session here
})

could be a little bit more specific about here? You know the thing is you call regenerate on session, but the updated object is not a session, but a request object… if I would call something like req.regenerateSession() it would make sense I think…

req.session is an object that is tightly coupled with req

I just wonder if the old session object can still be considered “consistent” and usable after the regeneration. It seems that the req object (while still tightly coupled) is no longer relevant to the old session…?

Always accessing as req.session is the only supported method for using sessions saving them to another variable is unsupported.

I think that mentioning this in documentation could help some stupid people like me. I would dare to claim that inablity to pass an object alone is not ususal and maybe it contradicts with some OOP principes (just some feelings…)?

Disclaimer: I don’t seek “satisfaction”, I am just sharing some thoughts … feel free to not reply and keep this closed … I respect and appreciate your work and contribution… Happy new year 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regenerate session IDs with Nodejs Connect - Stack Overflow
I'm trying to regenerate SIDs after a given interval to avoid session fixation. There's a method called req.session.regenerate which, according ...
Read more >
session_regenerate_id - Manual - PHP
session_regenerate_id() will replace the current session id with a new one, and keep the current session information. When session.use_trans_sid is enabled, ...
Read more >
synapse-session - npm
Session.regenerate(callback) ... To regenerate the session simply invoke the method. Once complete, a new SID and Session instance will be initialized at req....
Read more >
Storing User Sessions on the Server with Express-Session
regenerate (callback)​​ We call this to regenerate the session. Once it's called, a new session ID and Session instance will be initialized at...
Read more >
11 Session Pooling and Connection Pooling in OCI
Session pooling means that the application creates and maintains a group of stateless sessions to the database. These sessions are provided to thin...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found