Question

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.

Was it helpful?

Solution 2

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().

OTHER TIPS

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
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top