我有一个单节点服务器响应请求并根据主机头重定向用户.用法是静态/家庭网站位于www,每个用户都有自己的子域(即www.example.com和site.example.com).路由按照site.js.
当用户未登录时,他们被重定向到登录.
我发现当用户被重定向到他们的子域时,会话不会被维护.我想这是预料之中的,但我想知道是否有办法在两个子域中维持相同的会话.
我希望如果他们登录并返回到www.example.com,他们会看到一个不同的视图,其中包含一个指向注销/他们的仪表板等的链接.我现在的解决方法是,我想,只是创建他们的子域上的会话,如果他们确实返回到www,就好像他们没有登录一样.
任何人之前处理过这个问题或得到如何以这种方式处理会话的答案?
我认为问题可能在users.js中,我重定向到'http://site.example.com',因为它不是相对路径...
这是相关的代码(用户查找是使用MongoDB完成的,我把它留作工作正常 - 调用此服务的行是users.authenticate)...
server.js:
app.configure ->
app.set "views", "#{__dirname}/views"
app.set "view engine", "jade"
app.use express.bodyParser()
app.use express.methodOverride()
app.use express.cookieParser()
app.use express.session {
key: "KEY",
secret: "SECRET",
store: new MemoryStore(),
cookie: {
domain: 'example.com',
maxAge : 1000*60*60*24*30*12
}
}
app.use express.static "#{__dirname}/public"
app.use express.logger "short"
app.use express.favicon "#{__dirname}/public/img/favicon.ico"
app.use app.router
Run Code Online (Sandbox Code Playgroud)
site.js:
module.exports = (app) ->
app.get '/', (req, res) ->
console.log "/ hit with #{req.url} from #{req.headers.host}"
domains = req.headers.host.split "."
org = if domains …Run Code Online (Sandbox Code Playgroud) 我使用护照JS,express和mongoose来制作API.当我在同一个域中测试它时,它保持会话并正常工作.但是在跨域中它失败了.任何线索如何使用相同的配置在跨域维护会话.以下是代码
allowCrossDomain = function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", req.headers["access-control-request-headers"]);
// res.header("Access-Control-Allow-Credentials", "true");
if ("OPTIONS" == req.method) {
res.send(200);
} else {
next();
}
//allow all crossDomain request
app.use(allowCrossDomain);
//session handling
app.use(express.cookieParser("gallery"));
app.use(express.session());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
// check if client sent cookie
var cookie = req.cookies.cokkieName;
if (cookie === undefined) {
//set up cookie here by a random number
});
}
next(); // <-- important!
});
passport.use(new LocalStrategy({
usernameField: "email" …Run Code Online (Sandbox Code Playgroud)