服务器是具有快速会话,通行证,快速服务的nodejs
我要避免保存Cookie ,当用户未通过身份验证时,这可能吗?
var sessionStore = new session.MemoryStore;
app.use(session({
cookie: { maxAge: null,
httpOnly: true,
secure: true,
},
store: sessionStore,
resave: 'false',
secret: 'somthing',
name: "id",
saveUninitialized: false
}));
是否可以仅在用户成功登录后才存储cookie?谢谢!
答案 0 :(得分:0)
您必须创建一个快速会话:https://www.npmjs.com/package/express-sessions 然后像这样存储会话:
let session = require("express-session");
app.use(session({
secret: "secret",
resave: false,
saveUninitialized: true,
cookie: {secure: true,
httpOnly: true,
maxAge: 1000 * 60 * 60 * 24
}
}));
此会话将在您访问网页期间存储。如果要为cookie设置值,只需在请求中请求cookie并设置某种值:
router.route("/login")
.post(function(req, res) {
req.session.Auth = req.body.user // => user values?
})
或 您可以使用cookie-session(https://www.npmjs.com/package/cookie-session)
如果您使用cookie会话,那么如果您对会话变量或从服务器端进行了任何更改,则无需重新启动服务器。
app.use(cookieSession({
name: 'session',
keys: ['key1', 'key2']
}))
app.get('/', function (req, res, next) {
// Update views
req.session.views = (req.session.views || 0) + 1
// Write response
res.end(req.session.views + ' views')
})