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.

Option to save session before sending response

See original GitHub issue

Looks like fix for https://github.com/expressjs/session/issues/61 (https://github.com/expressjs/session/commit/901c8e062c8789d3963ebe175a75d25d3c229245) is causing some wired race condition. In my usecase req.session.save is calling also encrypting the data before storing. And usually it takes more little longer than non-encrypted version. Because of the async nature of session save the next request is starting most of the time before the save is complete. And this next request is fetching the old version of session. Is there any possibility to add some flag to control the async/sync session save or any other solution?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:30 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
dougwilsoncommented, Aug 17, 2014

just in case you’re not aware and feel like you have to wait on me, there is a work-around, which is to call req.session.save yourself and respond within that callback. for example

app.post('/', function (req, res, next) {
  // do stuff
  req.session.email = email
  req.session.save(function (err) {
    if (err) return next(err)
    res.render('page')
  })
})
0reactions
mjquitocommented, Nov 25, 2017

I still have the same issue as @skoranga was having it. The res.session.save() is saving twice. I’m using “express”: “4.16.2”, “express-session”: “1.15.6”. I really need help!

I’m saving my sessions on files, I’m using the session-file-store. The problem I’m trying to solve is that when I call “res.redirect(‘/’)”, I noticed in the “./express-session/index.js”, the res.end() gets ran and inside of that, the following gets ran

...
if (shouldSave(req)) {
        req.session.save(function onsave(err) {
          if (err) {
            defer(next, err);
          }

          writeend();
        });
...

And then the req.session.save runs asynchronously which then the redirection to \ starts happening and the session gets loaded BUT the req.session.save hasn’t finished saving the session in the file which then causes to load the old or previous state of the session. I used the suggested workaround (see below) of calling req.session.save before the redirecting

app.post('/', function (req, res, next) {
  // do stuff
  req.session.email = email
  req.session.save(function (err) {
    if (err) return next(err)
    res.render('page')
  })
})

And it works, but, it is calling twice to the req.session.save which is a no no. I really need help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJs express-session don´t save the session - Stack Overflow
I´ve rewrite the code like this: app.post('/login', function(req, res) { console.log("Before: "); console.log(sess); sess = req.session; ...
Read more >
Express session middleware
Create a session middleware with the given options . Note Session data is not saved in the cookie itself, just the session ID....
Read more >
Session Management in Node.js using ExpressJS and ...
This tutorial will help the reader develop a session based authentication system and go over how to manage sessions using Express.js and ...
Read more >
Manage the Skill Session and Session Attributes | Alexa Skills ...
Save data during the session. To retain data during the session, use session attributes. Create a map of key/value pairs with the data...
Read more >
How to Manage Session using Node.js and Express
If you want to send Anything to Nodejs server, you can do it using GET or POST HTTP method. Now as per your...
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