Помогите, пожалуйста, уже все что можно перепробовал. Я хочу сделать авторизацию через Passport.js используя FacebookStrategy. Код на сервере:
//... подключение модулей
var app = express();
app.use(cors()); //этот модуль должен разрешить кроссдоменные запросы,
//но он почему-то не работает
var FacebookStrategy = require('passport-facebook').Strategy;
var FACEBOOK_APP_ID = "***************"; //тут вставлены мои ключ
var FACEBOOK_APP_SECRET = "******************************"; //и секрет
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "http://localhost/auth/facebook/callback"
},
function (token, refreshToken, profile, done) {
process.nextTick(function() {
new User({'id_facebook': profile.id}).fetch().then(function (user) {
if (user) {
return done(null, user); // user found, return that user
} else {
var u = {
id: uuid.v4(),
nick: profile.emails[0].value.split('@')[0],
name: profile.displayName,
email: profile.emails[0].value,
pswhash: bcrypt.hashSync('facebook'),
id_facebook: profile.id
};
knex('users').insert(u)
.then(function () {
new User({'id': u.id})
.fetch()
.then(function (user) {
return done(null, user);
})
.catch(function (err) {
done(err);
});
})
.catch(function (err) {
done(err);
});
}
})
.catch(function (err) {
done(err);
});
});
}));
//...другие middleware
app.get('/auth/facebook',
passport.authenticate('facebook', {scope: ['email', 'user_birthday', 'user_likes']}),
function (req, res) {
});
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {failureRedirect: '/main'}),
function (req, res) {
res.redirect('/users/' + req.user.id);
});
Код на клиенте (AngularJS, кнопка, по клику вызывает функция ниже):
$scope.loginWith = function(social) {
$http.get('/auth/' + social).success(function(data) {
console.log(data);
});
};
Когда я в адресную строку прописываю localhost/auth/facebook, то авторизация срабатывает! А когда по кнопке нажимаю - выдает ошибку:
XMLHttpRequest cannot load https://www.facebook.com/v2.2/dialog/oauth?response_type=code&redirect_uri=…allback&scope=email%2Cuser_birthday%2Cuser_likes&client_id=490483854451281. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.