我正在使用passport-facebook登录我的用户,当facebook调用myt回调时,我进入了未定义的循环。我已经阅读了很多网站和问题,即使在这里,但真的,我找不到任何解决方案。
这是代码,我将把它全部放在这里,就像放到一个单独的文件中一样:
var restify = require('restify');
var passport = require('passport-restify');
var FacebookStrategy = require('passport-facebook').Strategy;
var server = restify.createServer();
server.use(restify.bodyParser());
passport.use(new FacebookStrategy({
clientID: 'APP_ID',
clientSecret: 'APP_SECRET',
callbackURL: 'http://192.168.0.13:8080/login/facebook/callback'
}, function (token, refreshToken, profile, done) {
//this code never is executed, why?
return done(null, profile);
}));
server.use(passport.initialize());
server.get('/login/facebook', function(req, res, next) {
passport.authenticate('facebook', { display: null, scope: ['email']})(req, res, next);
});
server.get('/login/facebook/callback', (req, res, next) => {
//here is where the loop happends, i´m getting into this and never can go out from here
passport.authenticate('facebook', function(profile) {
//I could not execute this
if (!profile || !profile.id) {
return res.json(500, 'We had trouble signing you up with Facebook. Please try again or sign-up via email.');
} else {
res.json(200, profile);
}
})(req, res, next);
});
server.listen(config.port, function () {
console.log('%s listening at %s', server.name, server.url);
});真的,我很沮丧,我现在,我现在,有成千上万的帖子在谈论这件事…我都试过了,我还是来了。
谢谢。
发布于 2016-09-10 00:28:56
过了一段时间,我发现了问题。我发布的是多次测试的结果。
问题是我用了一种不好的方式来使用剖腹产。简单地说,我是这样做的:
server.get('/login/facebook/callback', passport.authenticate('facebook', (req, res, next) => { /* magic here*/ });因此,执行next()的回调就像传递到authenticate方法中一样。这并不是因为我不理解这个问题,而是我没有意识到错误在哪里。
正确的方法应该是:
server.get('/login/facebook/callback', passport.authenticate('facebook'), (req, res, next) => { /* magic here */ });感谢大家的合作。
https://stackoverflow.com/questions/39218270
复制相似问题