嗨,我有以下代码。在完成req.login
之后,req._passport
在那里有了正确的信息。但是,在我的isAuthenticated
函数内部调用res.redirect(“ / profile”)之后,req._passport
是未定义的,但是req.session.passport.user
具有正确的信息。我查看了req.isAuthentication方法,它正在寻找req._passport
。为何在重定向后未定义req._passport
以及为什么同时存在req._passport
和req.session.passport
?谢谢!
app.post('/signin', function (req, res, next) {
passport.authenticate('local-signin', function (err, user, info) {
......
req.login(user, function (err) {
console.log(req._passport); // Here req._passport is set.
return res.redirect('/profile');
});
});
})(req, res, next);
});
app.get('/profile', isAuthenticated, profile.index);
function isAuthenticated(req, res, next) {
console.log(req.session.passport.user); // looks good
console.log(req._passport) // undefined
if (req.isAuthenticated()) {
console.log("authenticated"); // Not reaching here
return next();
}
console.log("not authenticated"); // reaching here
res.redirect('/signin');
}