Sono stati due giorni e un milione di tentativi per abilitare CORS quando si tenta di autenticare un utente con Facebook utilizzando Passport in NodeJS/Express.Problemi di dominio incrociato Angolare/Nodo/Express/Passport - Abilitare l'autenticazione Facebook CORS Passport
L'errore che ottengo su Chrome è questo:
XMLHttpRequest cannot load https://www.facebook.com/dialog/oauth?response_type=code&redirect_uri=http%…%3A8080%2Fauth%2Ffacebook%2Fcallback&scope=email&client_id=598171076960591.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8080' is therefore not allowed access.
I percorsi che uso sono così semplice come sembra:
// =====================================
// FACEBOOK ROUTES =====================
// =====================================
// route for facebook authentication and login
app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
// handle the callback after facebook has authenticated the user
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/home',
failureRedirect : '/login'
}));
In questo modo il percorso viene chiamato sul mio file angularJS (I 'ho anche provato withCredentials impostazione: true):
$http.get('/auth/facebook')
.success(function(response) {
}).error(function(response){
});
che ho provato una dozzina di soluzioni che ho trovato qui su StackOverflow e altri forum.
ho provato ad aggiungere questo sul davanti ai miei percorsi sui file routes.js:
app.all('*', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header("Access-Control-Allow-Headers", "Content-Type,X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name"); res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,HEAD,DELETE,OPTIONS'); res.header('Access-Control-Allow-Credentials', true); if ('OPTIONS' == req.method) { res.send(200); } else { next(); } });
Ho provato ad aggiungere questo su file di server.js (notare che ho cambiato intestazione SetHeader ma io 'ho provato entrambi):
app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type,X-Requested-With'); res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,HEAD,DELETE,OPTIONS'); res.setHeader('Access-Control-Allow-Credentials', true); if ('OPTIONS' == req.method) { res.send(200); } else { next(); } }); require('./app/routes.js')(app, passport);
ho provato ad aggiungere questo sul mio file app.js (angularJS configurazioni):
$httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; $httpProvider.defaults.withCredentials = true;
In ogni caso, non so cos'altro fare. Tutto ciò che ho trovato online non ha funzionato. C'è una possibilità che abbia qualcosa a che fare con me usando AngularJS Routing? Non vedo alcuna ragione per cui ciò possa avere importanza, ma mi sono perso le supposizioni.
La mia situazione è molto simile a questa: Angular/Node/Express/Passport - Issues when connecting to facebook(CORS)
Grazie in anticipo!
possibile duplicato di [? Come consentire CORS in veloce/NodeJS] (http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express -nodejs) –
So che ci sono molte domande simili, e ho visto quasi tutte. Sfortunatamente, nessuna delle soluzioni che ho provato ha funzionato finora ... –
Non è possibile utilizzare contemporaneamente "Access-Control-Allow-Credentials: true" e "Access-Control-Allow-Origin: *" ". Per il tuo caso, dovresti impostare "Access-Control-Allow-Origin: localhost: 8080' anziché" * ". – wuct