node.js - req.session undefined in route -
i'm building simple site expressjs , passportjs , i'm running problem can't access session variables in routes.
there lot of threads topic solutions don't work me. looks error somewhere else.
my app configured this:
app.configure(function() { app.set('port', process.env.port || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.logger()); app.use(express.cookieparser()); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(express.session({ path: '/', secret: 'very secret' })); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); app.use(express.static(__dirname + '/public')); });
once passport verified twitter account it's redirected site:
app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureredirect: '/login' }), function(req, res) { res.redirect('/'); console.log("populate session") populatesession(req, res); });
this works, i'm seeing "populate session" output in console if i'm logged in.
the populatesession()
looks this:
function populatesession(req, res) { user.findone({ twitterid: req.user.id }, function(err, result) { if (result) { // store data session req.session.twitteraccesstoken = result.twitteraccesstoken; req.session.twitteraccesstokensecret = result.twitteraccesstokensecret; req.session.lastfmaccountname = result.lastfmaccountname; console.log("in session: " + req.session.lastfmaccountname) } }) }
"in session" printed right way. session works. problem want have access session variables in routes because want pass them view templates this:
app.get('/account', ensureauthenticated, function(req, res) { console.log(req.session) console.log("lastfm nick " + req.session.lastfmaccountname) res.render('account', { user: req.user, lastfmnick: req.session.lastfmaccountname });
that's i'm running problems. req.session
contains twitter fields passport populating req.session.lastfmaccountname
undefined
.
any idea what's wrong there or there better way pass variables view? feel it's not idea have db queries fields in routes if stored in session.
thanks!
the session automatically saved when response ends, in case res.redirect()
, done before modifications made session.
function(req, res) { res.redirect('/'); // ends response, saving session populatesession(req, res); // modifies session without saving });
since .findone()
asynchronous, if revise populatesession()
take , call callback when find completes, can control order session modified first:
function populatesession(req, res, next) { user.findone({ twitterid: req.user.id }, function(err, result) { if (result) { // ... } if (next) next(err); }) }
app.get('/auth/twitter/callback', /* ... */, function(req, res) { populatesession(req, res, function () { res.redirect('/'); }); });
it allows use populatesession
middleware:
app.get('/auth/twitter/callback', /* ... */, populatesession, function(req, res) { res.redirect('/'); });
Comments
Post a Comment