如何处理PassportJS端点错误

时间:2013-12-18 00:31:44

标签: node.js express passport.js

如果我在http://localhost/login?code=***

访问Facebook回调链接

伪造的代码:FacebookTokenError: Invalid verification code format.
使用相同的代码(重放攻击):FacebookTokenError: This authorization code has been used.
使用过期的代码:FacebookTokenError: This authorization code has expired.

所有这些错误都与客户无关。我想简单地重试登录过程(再次重定向到Facebook进行身份验证)。

Express().get('/login', Passport().authenticate('facebook', {
    failureRedirect: '/',
}), function(req, res) {

});

但是,如果出现上述三个错误,服务器只会抛出错误并发送给客户端。

端点错误是否有错误回调?

1 个答案:

答案 0 :(得分:1)

以下是我正在使用的内容:

// Google OAUTH sendoff
app.get('/auth/google',
  passport.authenticate('google')
);

// Google OAUTH return
app.get('/auth/google/return', function(req, res, next) {
  passport.authenticate('google', function(err, user, email) {
    if (err) { return next(err); }

    // OAUTH success, but user isn't authorized
    if (!user && email) {
      return res.redirect('/myNotAuthorizedUrl');
    // OAUTH error
    } else if (!user) {
      return res.redirect('/login/');
    // user disabled or some other check
    } else if (user.get('is_blacklisted') !== 1) {
      return res.redirect('/youAreEvil');
    }
    // success and authorized
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.redirect('/home');
    });

  })(req, res, next);
});