Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

res.redirect happening before req.flash #195

Closed
franckl opened this issue Sep 21, 2015 · 4 comments
Closed

res.redirect happening before req.flash #195

franckl opened this issue Sep 21, 2015 · 4 comments

Comments

@franckl
Copy link

franckl commented Sep 21, 2015

Hi Jerome,

It seems connect-mongo is causing the issue referenced below :
jaredhanson/connect-flash#23 (comment)

What do you think ?

@behcet
Copy link
Contributor

behcet commented Sep 21, 2015

It's a race condition. User gets redirected to new page before session gets saved on database.

Try executing a req.session.save(callback) after calling req.flash and wait for save callback to redirect to user.

@behcet
Copy link
Contributor

behcet commented Oct 10, 2015

@franckl Any updates ?

@jdesboeufs
Copy link
Owner

Ok it's not related to connect-mongo.
connect-flash should be fixed to ensure session has been saved (whatever the session store used).

@malcolmocean
Copy link

For people who come across this in future searching...

I was halfway through rolling my own new version of req.flash, when I was looking through the docs of express-session and came across this gem:

Note Since version 1.5.0, the cookie-parser middleware no longer needs to be used for this module to work. This module now directly reads and writes cookies on req/res. Using cookie-parser may result in issues if the secret is not the same between this module and cookie-parser.

And lo, I had these lines:

app.use(require('cookie-parser')())
const session = require('express-session')
const MongoStore = require('connect-mongo')(session)
app.use(session({
  secret: process.env.SESSION_STORE_SECRET,
  store: new MongoStore({mongooseConnection: mongoose.connection}),
  maxAge: 10*365*24*60*60*1000, // set to 10 years
  resave: false,
  saveUninitialized: false
}))

Once I changed the cookie-parser line to:

app.use(require('cookie-parser')(process.env.SESSION_STORE_SECRET))`

it worked exactly as expected!

(For some people, the answer will be to remove cookie-parser altogether.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants