使用快速会话和Cookie时,我遇到了一些问题。在我拆分服务器之前,我可以通过执行以下操作在会话中设置用户ID:req.session.uuid = user._id.toString()
。但是,现在我使用cors时这不起作用。有什么想法吗?
快速会话设置:
var exSession = module.exports.exSession = expressSession({
secret: options.expressSessionSecret,
store: new redisStore({
host: 'localhost',
port: 6379,
client: redisClient,
ttl: options.sessionTTL
}),
proxy: true,
signed: true,
resave: true,
saveUninitialized: true,
cookie: {
secure: true,
maxAge: 36000000
},
rolling: true,
unset: 'destroy'
})
app.use(exSession)
Cors设置:
const whitelist = ['http://localhost:3000']
const corsOptions = {
origin: function (origin, callback) {
var originIsWhitelisted = whitelist.indexOf(origin) !== -1
callback(originIsWhitelisted ? null : 'Bad Request', originIsWhitelisted)
},
credentials: true
}
app.use(cors(corsOptions))
登录时(此部分不再起作用):
req.session.uuid = user._id.toString()
这就是解析后的cookie看起来像:
req.session Session {
cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true },
uuid: '58b3a28faf819d0de26bc90a'
}
现在看起来像这样:
req.session Session {
cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true },
uuid: undefined
}