我是Express会议的新手,并希望我可以使用快速会话传递变量。
基本上我使用护照来允许GitHub登录,我可以使用req.user
获取用户的信息。当用户通过GitHub登录时,我想将用户重定向到登录页面,该页面会要求用户在创建帐户之前验证信息,或者如果帐户已经存在,则会显示欢迎页面。
let app = express();
app.use(session({secret: "blahblahblah"}));
console.log('github authenticated');
let sess = req.session;
//I want to pass the req.user(returned from GitHub) to a variable sess.user
sess.user = req.user;
console.log(sess.user); //shows correct info
res.redirect('/user/login');
module.exports = app;
然而,当我尝试从我的路线访问req.session时,它返回'undefined':
let session = require('express-session');
let app = express();
app.use(session({secret: "blahblahblah"}));
app.use(router);
router.get('/login', function(req, res, next) {
let sess = req.session;
console.log(sess.user); //undefined
//would check the database
//if user doesn't already exists pre-populate fields
//render appropriate page
});
答案 0 :(得分:1)
您应该在请求对象本身传递数据
let sess = req.session;
sess.user = req.user;
req.mysess=sess.user //pass like this
router.get('/login', function(req, res, next) {
let sess = req.mysess;
console.log(sess);
});