我遇到了传递会话变量的问题。我相信它们甚至不存储在MongoDb中,因为db.sessions.find()是空的。 我的模块是
"express": "~4.12.2","express-session": "~1.11.1","mongoose": "*",
"passport": "~0.2.1", "passport-local": "~1.0.0",
"passport-local-mongoose": "~1.0.0"
我的app.js有
app.use(cookieSession({
keys: "myapp.sid",
secret: env.get("SESSION_SECRET"),
saveUninitialized: true,
resave: true,
cookie: { secure: true },
store: new MongoStore({ mongooseConnection: mongoose.connection,
touchAfter: 24 * 3600
})
}));
function ensureSecure(req, res, next){
if(req.secure){
return next();
};
res.redirect('https://'+req.hostname+':' + 8000 + req.url);
};
app.all('*', ensureSecure);
app.use(csrf({ cookie: true }));
app.use(function(req, res, next) {
res.locals.csrftoken = req.csrfToken();
res.locals.session = req.session;
// flash doesnot get passed, maybe same problem
res.locals.sessionFlash = req.session.sessionFlash;
delete req.session.sessionFlash;
next();
});
它出现在我的路线之前。 在登录路线中我有:
router.post('/login', function (req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err);}
if (!user) { ..do something}
var theSchema = mongoose.model('OneSchema');
theSchema.findOne({'oneschema': req.body.dbschema}, function(err, oneschemas) { // go back and fetch some data,
req.session.role = user.role;
req.session.name = user.username;
console.log("session : %j",req.session); // OK full info
return res.redirect('/settings');
}); // findone
})(req, res, next); // end passport line
}); //end login route
现在设置有:
router.get('/settings', function(req, res) {
console.log("session : %j",req.session); // BOUM!!!!!
res.render('settings');
});
BOUM !!:会话:{“护照”:{}}
我从昨天开始一直在寻找周围的一切。我尝试了所有的想法。但我无法做到这一点。 我非常感谢能够帮助我找到解决方案的指示。 感谢