2013-09-27 155 views
8

Tôi đang sử dụng JS hộ chiếu, express và mongoose để tạo API. Khi tôi kiểm tra nó trong cùng một tên miền, nó duy trì phiên làm việc và hoạt động tốt. Nhưng trong miền chéo nó không thành công. Bất kỳ đầu mối làm thế nào tôi có thể duy trì phiên trong miền chéo bằng cách sử dụng cùng một cấu hình. Dưới đây là đoạn codeSố hộ chiếu không duy trì phiên trong miền chéo

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" 
}, 
function(email, password, done) { 
    User.authenticate(email, password, function(err, reply) { 
     //authenticate user and call the callback 
      return done(err, false); 

    }); 
})); 


passport.serializeUser(function(user, done) { 
return done(null, user._id); 
}); 


passport.deserializeUser(function(id, done) { 
//find user via id and return the user details 
return done(null, user._id); 
}); 

    app.post("/login", function(req, res, next) { 
    passport.authenticate("local", 
     function(err, data, info) { 
      //custom callback 
      user.getProfile(req, res, next, err, data, info); 
     })(req, res, next); 
}); 
+0

@ kundu_ bạn đã nhận được giải pháp? –

Trả lời

2

Cho phép các thông tin được chia sẻ bằng cách thiết lập Access-Control-Allow-Credentials tiêu đề. (Tôi không chắc chắn lý do bạn đã nhận xét trong mã của mình)

res.header("Access-Control-Allow-Credentials", "true"); 

sau đó pass the credentials từ javascript qua XHR đối tượng.

xhr.withCredentials = true; 
6

Tôi đã gặp sự cố tương tự. Trước khi định cấu hình bất kỳ điều gì trong ứng dụng nhanh, hãy sử dụng thông tin sau (giống hệt) để đặt tiêu đề phản hồi cho tên miền chéo:

app.use(function(req, res, next) { 
res.header('Access-Control-Allow-Credentials', true); 
res.header('Access-Control-Allow-Origin', req.headers.origin); 
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); 
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'); 
if ('OPTIONS' == req.method) { 
    res.send(200); 
} else { 
    next(); 
} 
}); 

Nó làm việc cho tôi. May mắn nhất!

+0

Bạn có biết tại sao nó phải theo cách này? Tại sao thông tin đăng nhập được yêu cầu? –

+0

Bạn thật tuyệt vời. Tôi lãng phí 6 giờ cho việc này. Cảm ơn người đàn ông –

+0

Tôi cũng phải đối mặt với cùng một vấn đề và giải pháp này vẫn không làm việc cho tôi. Tôi nên làm gì? Hãy giúp tôi. –

4

Như mỗi câu trả lời Sriharsha của:

  • Set res.header("Access-Control-Allow-Credentials", "true");

  • Hãy chắc chắn rằng bạn vượt qua các chứng chỉ trong các cuộc gọi phía khách hàng. Ví dụ cho AJAX, thêm video này vào cuộc gọi của bạn: xhrFields: {withCredentials: true},

Ngoài ra:

  • Không sử dụng ký tự đại diện cho Access-Control-Allow-Origin với một yêu cầu chứng nhận

    explained on MDN:

    khi trả lời một yêu cầu chứng nhận, máy chủ phải xác định một miền , và không thể sử dụng hoang dã chải


tôi sử dụng tập tin này, và gọi nó là từ mô-đun chính của tôi với require("./enable-cors.js")(app);

// enable-cors.js 
module.exports = function(app) { 

    var methodOverride = require('method-override') 
    app.use(methodOverride()); 
    var allowCrossDomain = function(req, res, next) { 
     res.header('Access-Control-Allow-Credentials', true); 
     res.header('Access-Control-Allow-Origin', req.headers.origin); 
     res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); 
     res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); 

     // intercept OPTIONS method 
     if ('OPTIONS' == req.method) { 
      res.send(200); 
     } 
     else { 
      next(); 
     } 
    }; 
    app.use(allowCrossDomain); 
    // Built upon: http://cuppster.com/2012/04/10/cors-middleware-for-node-js-and-express/#sthash.WdJmNaRA.dpuf 

}; 
+0

Cảm ơn bạn rất nhiều. –