在我的用户经过身份验证之后,我尝试将一些用户信息存储在req.session.user中。当我拥有用户信息时,只需编写req.session.user = user并进行重定向。然后,我尝试获取这些信息,但我只能得到一个空会话。
这是我的会话设置:
app.use(expressSession({
secret: process.env.SESSION_SECRET,
saveUninitialized: false,
resave: true,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
这是我的发布路线,其中我在req.session中设置了信息(console.log显示用户数据)
router.post('/verify', (req, res) => {
async function verify() {
//Some code
User.findOne({googleId: googleId}, (err, user) => {
if(!user) {
let user = new User({
//...
});
user.save((err, user) => {
if (err) throw err;
console.log('--------------------------------------------------');
console.log('New user');
req.session.user = user;
console.log(req.session);
});
} else {
console.log('--------------------------------------------------');
console.log('Already user');
req.session.user = user;
console.log(req.session);
}
});
}
res.json('/')
}
verify().catch(console.error);
});
在这里,我尝试获取用户数据,而req.session.data为空:
router.get('/', categoryController.category_all, projectController.project_all, (req, res) => {
console.log('Home ----------------------------------');
console.log(req.session);
res.render('project/accueil', { data: res.locals });
});