0
基本上,當我停止服務器並啓動備份時,我檢查是否已登錄,並檢查是否有附加到我的Cookie會話的用戶標識。它總是返回false。出於某種原因,當我關閉我的服務器時,用戶名也附加了我的cookie會話似乎被刪除。有沒有辦法阻止它被刪除?我希望能夠關閉服務器,然後再打開它,並讓connect.sid cookie包含一個user.id,允許我在登錄時自動重定向。當我啓動服務器備份時,req.user未定義
這裏是我創建會話的位置:
app.use(session({
secret: 'secret',
saveUninitialized: true,
cookie: { expires: new Date(253402300000000) }
}));
這裏是我附上一個用戶ID會話:
app.post('/signin', passport.authenticate('local', {
failureRedirect: '/',
failureFlash: true
}),
function (req, res) {
// If this function gets called, authentication was successful.
// `req.user` contains the authenticated user.
res.redirect('/');
});
這裏就是我檢查,如果用戶登錄或不:
app.get('/', function (req, res, next) {
var username;
if (req.user || req.isAuthenticated()) {
username = req.user.username
} else {
username = '';
}
res.render('StockPage/HomePage', { user: username });
next();
});
你必須堅持你的會話存儲到磁盤,您可以通過使用一些特定的服務器,如Redis的爲實現這一目標@Tuan在下面回答。 Express在內存中使用會話,如果您關閉服務器,則會丟失所有數據。 – bpinhosilva