Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

nowjs.sessions is empty #184

Open
ghost opened this Issue · 5 comments

2 participants

@ghost

I'm using Express sessions, and can read session data from req.session in the Express application object. However, I'm not able to read data from this.user.session because nowjs.sessions is never populated. The callback function in this block of code in now.js (starting line 210) never seems to get invoked:

if('use' in server && 'stack' in server && 'route' in server) {
    server.use(function(req, res, next) {
       self.sessions[req.sessionID] = req.session;
       next();
    });
}

See issue #175 which may be related / identical. Thanks.

@ghost

Using v0.8.1

Can anyone confirm this issue, or am I doing something wrong? Thanks.

@lukifer

I'm experiencing this as well. If I figure out a fix, I'll post it here. I'm suspecting something to do with the order in which middleware is declared.

@lukifer

I haven't identified the real root cause, but I've isolated the issue to static routing:

app.use(express.static(__dirname + '/public'));

This seemed to prevent session functionality regardless of when it was loaded. I was able to sidestep the issue by using routes instead and disabling the static handler. I'd love to have a better fix, though.

@ghost

lukifer, thanks for looking at this. For me, disabling static routing has no effect -- nowjs.sessions and this.user.session remain empty. Below is the block of code I use to create the Express server. I've tried disabling static routing, methodOverride(), and responseTime() but none of these had any effect.

var express = require('express');
var app = module.exports = express.createServer();
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'eco');
app.set('view options', {layout: false});
app.use(express.bodyParser({
uploadDir: '/tmp/node',
keepExtensions: false
}));
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: '****', store: sessionStore }));
app.use(express.responseTime());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});

var nowjs = require('now');
var everyone = nowjs.initialize(app, { cookieKey: 'connect.sid' });

@wired8

Here is a fix which works for me:

Setup your sessionstore:

// use redis as session middleware
sessionStore = new RedisStore({'db':'1', maxAge: 1209600000});

nowjs.sessions(sessionStore);
nowjs.sessionStore = sessionStore;

Modify the user now user module:

now/lib/user.js

Replace this:

// Populate session by parsing cookie
var cookie = this.user.cookie[nowjs.options['cookieKey']];
if (cookie) {
this.user.session = nowjs.sessions[unescape(cookie)];
}

With this:

var sid = decodeURIComponent(this.user.cookie['connect.sid']);
if (sid) {
var self = this;
nowjs.sessionStore.get(sid, function (err, session) {
self.user.session = session;
return;
});
}

You should now have session info in your user object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.