node.js 护照会话 cookie 域 [英] node.js passport session cookie domain
问题描述
我正在使用 node.js + 护照,并试图弄清楚如何在父域上设置 cookie,以便它可用于子域.
I'm using node.js + passport and trying to figure out how the set the cookie on the parent domain so that it's available to sub domains.
用户登录到 Domain.com用户然后转到 Sub.Domain.com ......他应该仍处于登录状态.
User logs into Domain.com User then goes to Sub.Domain.com ... he should still be logged in.
如何在父域上设置 cookie?这是我目前拥有的.
how does one set the cookie on the parent domain? Here's what I currently have.
app.use(express.session({
secret: 'XXXXX',
store: new mongoStore({ url: app.get('mongodb-uri') })
}));
app.use(passport.initialize());
app.use(passport.session());
推荐答案
您需要做的是设置会话 cookie 的域.您应该可以这样做:
What you need to do is set the domain of the session cookie. You should be able to do this like:
app.use(express.session({
secret: <session_secret> ,
store: <session store> ,
cookie: {
path: '/',
domain: '.domain.com',
maxAge: 1000 * 60 * 24 // 24 hours
}
}))
请注意,域设置为 .domain.com(开头的点),这应该使其可用于所有子域.
Notice that the domain was set to .domain.com (the dot at the beginning) which should make it available to all subdomains.
这篇关于node.js 护照会话 cookie 域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!