node.js - Session variables in node express-session don't persist to the next request -
i using basic node express-session setup memory store , have code on server:
app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveuninitialized: true, cookie: { secure: true } })); app.post('/api/login', function(req, res) { req.session.username = req.body.username; } app.get('/api/getprofile', function(req, res) { user.findone({'username' : req.session.username}, function (err, userprofile) { console.log('getprofile executed user:' + req.session.username); if (err) throw err; console.log(userprofile); }); });
the problem req.session.username getprofile route undefined, although not in previous request login route. inspected http headers , strangely there no headers dealing cookies, server or client. right have no idea problem.
you cookie: { secure: true }
, web server on secure connection? if not, cookie won't written.
from docs:
please note secure: true recommended option. however, requires https-enabled website, i.e., https necessary secure cookies. if secure set, , access site on http, cookie not set.
Comments
Post a Comment