【问题标题】:Node.js passport-facebook callback loopingNode.js 护照-facebook 回调循环
【发布时间】:2016-09-10 00:28:56
【问题描述】:

我正在使用 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);
});

真的,我很沮丧,我现在,我现在,有成千上万的帖子在谈论这个......我尝试了所有这些,无论如何我都在这里......

谢谢。

【问题讨论】:

  • 你能展示你的序列化/反序列化函数吗?
  • passport.serializeUser(function (profile, done) { done(null, profile); }); passport.deserializeUser(function (profile, done) { done(null, profile); });
  • 在 serializeUser 中尝试用这个done(null, profile.id) 替换这个done(null, profile)
  • 我得到了相同的结果...同一行中的无限循环。非常感谢
  • 你的服务器在 nginx 上运行?

标签: javascript node.js passport.js passport-facebook


【解决方案1】:

过了一会儿,我发现了问题。我发布的是多次测试的结果。

问题是我使用了错误的括号。 最简单的是,我是这样做的:

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 */ });

感谢大家的合作。

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 2019-01-06
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    相关资源
    最近更新 更多