node.js - NodeJS Passport - Facebook 身份验证

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

我创建了一个 Facebook 应用程序,并且只有我一个人使用。现在我已经上线了,但除了我之外没有人能够登录。

他们收到以下错误。

FacebookTokenError: This authorization code has been used.
 at Strategy.parseErrorResponse (/var/www/html/project/node_modules/passport-facebook/lib/strategy.js:199:12)
 at Strategy.OAuth2Strategy._createOAuthError (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/lib/strategy.js:345:16)
 at /var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/lib/strategy.js:171:43
 at /var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:177:18
 at passBackControl (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:123:9)
 at IncomingMessage.<anonymous> (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:143:7)
 at IncomingMessage.EventEmitter.emit (events.js:117:20)
 at _stream_readable.js:920:16
 at process._tickCallback (node.js:415:13)

这里可能有什么问题。应用程序已上线。

最佳答案

我在 Express 中有一个 REST api,它使用来自 Jeroen Pelgrims 的信息进行无 session Facebook 登录。它的作用是:

使用 Facebook 策略登录 将 token 和信息保存在用户数据库中 使用该 token 进行承载登录 Passport-facebook 验证在回调页面中使用的是这样的: Passport.authenticate('策略', 选项, 用户, 错误)

该行为正确地引发了该错误!正如 @jsilveira 在评论中所说,如果用户登录两次就会发生错误......

我的答案很简单,我发现了错误...继续阅读,有一个但是...

//Facebook 身份验证和登录路由

 router.get('/auth/facebook',
        passport.authenticate('facebook', {session: false, scope : ['email'] })
    );

// handle the callback after facebook has authenticated the user
router.get('/auth/facebook/callback',
    passport.authenticate('facebook',  { session: false, failureRedirect : '/'}),

    // on succes
    function(req,res) {
        // return the token or you would wish otherwise give eg. a succes message
        res.render('json', {data: JSON.stringify(req.user.access_token)});
    },

    // on error; likely to be something FacebookTokenError token invalid or already used token,
    // these errors occur when the user logs in twice with the same token
    function(err,req,res,next) {
        // You could put your own behavior in here, fx: you could force auth again...
        // res.redirect('/auth/facebook/');
        if(err) {
            res.status(400);
            res.render('error', {message: err.message});
        }
    }
);

但是,如果你们希望它再次登录/授权,您可以在错误部分插入回调或重定向。

希望这能澄清你们遇到的一些问题。如果您有任何疑问或要求,请不要害羞。信息在我的个人资料中。

PS:stackoverflow.com 有一个重新验证的答案,如下所示:

app.post('/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
      if (err) {
      return next(err); // will generate a 500 error
      }
      // Generate a JSON response reflecting authentication status
      if (! user) {
      return res.send({ success : false, message : 'authentication failed' });
  }
  return res.send({ success : true, message : 'authentication succeeded' });
  })(req, res, next);
});

关于node.js - NodeJS Passport - Facebook 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33241883/

相关文章:

javascript - 按年龄限制访问 FB 应用程序

ios - Http删除?删除点赞?

node.js - 如何使用passport.js正确实现serializeUser?

node.js - NodeJS : Express 3. 0 带有 connect-flash(按照 Passport 本地策略中的建议),仍然发现 req.flash 的异常

node.js - 使用Passport基于动态路由进行认证

node.js - 如何从Azure函数进行https调用?

jquery - 使用 "share_open_graph"Facebook UI 为测验结果创建动态共享对话框

node.js - 对这个流可读语法有点困惑

javascript - Node.js 和 Jade

node.js - 我可以使用 passport-google 回调来验证 android/ios 用户吗?