1

My session are not being cleared on AppFog. My setup is as follows:

Node.js (with express) mongoDB connect-mongo for the session store mongoHQ for hosting my db.

I've hooked up my local setup to use the mongoHQ db instance and all my sessions are being cleared great. But when running the app from AppFog the sesssions does not get cleared.

My session middleware:

 app.use(express.session({
    secret: 'my secret here',

    cookie: { maxAge: new Date(Date.now() + 360000)},
    store: new MongoStore({
        url: connectionEnv.dbConnection
    })
}));

And here is an example of using and clearing a session:

console.log(req.session.errors); // Session has a value as intended
res.render('contact', {
    user: username,
    email: email,
    messages: req.session.messages,
    errors: req.session.errors
});

req.session.messages = null;
req.session.errors = null; // The session is beeing cleared

console.log(req.session.errors); // Session is now null

But if I refresh the page now the session has somehow gotten it's value back. This only happens when running the app from AppFog.

Tim Santeford
  • 27,385
  • 16
  • 74
  • 101

2 Answers2

2

Express/Connect saves the session state prior to ending the response to the client (See Here). Make your changes before calling res.render and also destroy your session variables using delete.

Try this:

var errors = req.session.errors
var messages = req.session.messages

// Before Render
delete req.session.errors
delete req.session.messages

res.render('contact', {
    user: username,
    email: email,
    messages: errors,
    errors: messages
});
Tim Santeford
  • 27,385
  • 16
  • 74
  • 101
  • Thanks for your help. Got it working though. I'm using the session for flash messages so i can't delete them before i render the view. Is it better to use delete for sessions rather than null? – Matthis Stenius May 30 '13 at 19:49
  • 1
    You can pass a callback to render, but then you'll have to call res.send explicitly. http://expressjs.com/api.html#res.render – furydevoid May 31 '13 at 00:31
0

Okej found the real issue regarding this problem. It has to do with my cache. Seems like the sessions are being cached when i use maxAge on my public folder. After i removed the cache the sessions were being cleared as expected.

Anyone how an idea on how to disable cache for sessions but not for static content?

UPDATE

Got it working with cache now. By clearing the sessions in the res.render callback and than calling res.send() and passing in the html that is return from res.render().