Tôi đang thiết lập một auth quản trị cơ bản, khi người dùng đăng nhập, tôi gọi chức năng này trong authcallback:nodejs req.session nhanh không lưu trữ của tôi biến
function checkAdmin (req) {
Admin.findOne({ user : req.user.id },function (err, admin, count){
req.session.isAdmin = true;
console.log("session : %j",req.session);
})
}
Và sau đó tôi làm
res.redirect('/')
các mã bên trong bộ điều khiển chỉ số:
console.log("session : %j",req.session);
res.render('index', { title: 'Accueil',message: req.flash('info') });
Và đây là những gì tôi nhận được trong giao diện điều khiển:
session : {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path"
:"/"},"passport":{"user":"5079832df1e9a6485e000001"},"flash":{},"isAdmin":true}
session : {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path"
:"/"},"passport":{"user":"5079832df1e9a6485e000001"},"flash":{}}
Tôi không hiểu tại sao phiên không lưu trữ giá trị isAdmin.
Tôi đã đọc một số câu hỏi khác nói rằng app.use (app.router) nơi có thể là một nguyên nhân nhưng tôi làm:
app.use(cookieParser)
app.use(express.session({store: session_store }));
app.use(passport.initialize())
app.use(passport.session())
// routes should be at the last
app.use(app.router)
Nó phải được loại ngốc Tôi đoán ..
Có lẽ một cái gì đó như thế này có thể giúp: req.session.regenerate https : //github.com/senchalabs/connect/issues/681 – alditis
Vì vậy, nó có nghĩa là mỗi khi tôi muốn thay đổi/thêm một biến trong phiên nI phải sử dụng req.session.regenerate? –
Có lẽ bạn nên thực hiện chuyển hướng bên trong chức năng checkAdmin. –