2013-09-27 9 views
8

में सत्र बनाए रखने के लिए मैं उपयोग कर रहा हूँ पासपोर्ट जे एस, एक्सप्रेस और नेवला एक API बनाने के लिए विफल रहता है। जब मैं इसे उसी डोमेन में परीक्षण करता हूं तो यह सत्र बनाए रखता है और ठीक काम करता है। लेकिन क्रॉस डोमेन में यह विफल रहता है। कोई भी संकेत मैं उसी कॉन्फ़िगरेशन का उपयोग कर क्रॉस डोमेन में सत्र कैसे बनाए रख सकता हूं। निम्नलिखित कोडपासपोर्ट js क्रॉस-डोमेन

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

साथ अपने मुख्य मॉड्यूल से इसे कहते @ कुंडू_ क्या आपको समाधान मिला? –

उत्तर

2

है क्रेडेंशियल्स की स्थापना द्वारा साझा किया जा करने की अनुमति दें पहुंच-नियंत्रण-अनुमति दें-क्रेडेंशियल हैडर। जावास्क्रिप्ट से एक्सएचआर वस्तु के माध्यम से (मैं नहीं यकीन है कि क्यों आप अपने कोड में टिप्पणी की है हूँ)

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

तो pass the credentials

xhr.withCredentials = true; 
6

मुझे एक ही समस्या थी। एक्सप्रेस ऐप में कुछ भी विन्यस्त करने से पहले, का उपयोग करें (ठीक उसी) क्रॉस-डोमेन के लिए प्रतिक्रिया के शीर्षक सेट करने के लिए:

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(); 
} 
}); 

यह मेरे लिए काम करता है। शुभकामनाएँ!

+0

तुम जानते हो क्यों यह इस तरह से हो गया है क्या? प्रमाण पत्र क्यों आवश्यक हैं? –

+0

आप कमाल कर रहे हैं। मैं इसके लिए 6 घंटे बर्बाद कर दिया। धन्यवाद आदमी –

+0

मैं भी एक ही समस्या का सामना करना पड़ रहा हूँ और इस समाधान अभी भी मेरे लिए काम नहीं कर रहा। मुझे क्या करना चाहिए? कृपया मदद करे। –

4

श्रीहर्ष के जवाब के अनुसार:

  • सेट res.header("Access-Control-Allow-Credentials", "true");

  • सुनिश्चित करें कि आप ग्राहक के पक्ष कॉल में साख पारित करें। AJAX के लिए उदाहरण के लिए, अपने कॉल करने के लिए इस जोड़ें: xhrFields: {withCredentials: true},

इसके अतिरिक्त:

  • एक credentialed अनुरोध के साथ पहुंच-नियंत्रण-अनुमति दें-उत्पत्ति के लिए वाइल्डकार्ड का उपयोग न करें

    explained on MDN के रूप में:

    जब जवाब एक credentialed अनुरोध करने के लिए, सर्वर डोमेन निर्दिष्ट करना होगा, और


मैं इस फ़ाइल का उपयोग कंधी जंगली उपयोग नहीं कर सकते, और 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

बहुत बहुत शुक्रिया। –

संबंधित मुद्दे