jdesboeufs / connect-mongo

MongoDB session store for Express
MIT License
1.96k stars 341 forks source link

Updating variables stored in the req session can result in false values #462

Open piphansdk opened 1 year ago

piphansdk commented 1 year ago

Hi,

We are trying to use the session of a user to store variables which needs to be used in different pages. The issue with the solution, is that the session seems to have an issue always updating the session in mongoDB.

Process:

  1. The user will access a URL, the function of the URL is to have NodeJS to update a variable stored in res.user.someValue
  2. NodeJS will call res.redirect
  3. The user will access the redirected URL. NodeJS is processing the request and getting res.user.someValue. This value is not identical to the one just set in step 1.

The issue was thoroughly tested using an automated test. The test ran 100 times on a Mac m1, which resulted in 30 cases where req.user.someValue was the old variable. In the same test on an older windows machine the test failed 5 times out of 100.

We are using the following for setting up our session store.

app.use( expressSession({ secret: process.env.COOKIE_SECRET_KEY, cookie: { sameSite: "lax", secure: process.env.SECURECOOKIE == "TRUE", domain: process.env.COOKIEDOMAIN }, proxy: true, resave: true, saveUninitialized: true, store: MongoStore.create({ mongoUrl: config.databaseUri, mongoOptions: {loggerLevel: "debug"}, autoRemove: 'interval', autoRemoveInterval: 10 // Value in minutes (default is 10) }) }) );

We wanted to further debug everytime the session was stored in mongo and collected from mongo, but we simply cannot start the debugging in any format for the module.

We are using the following versions: NodeJS v. 18.12.1 MongoDB v. 5.0.14 "connect-mongo": "^4.6.0", "express": "4.x", "express-session": "^1.17.3",

we'd appreciate any help we can get. Thanks