【问题标题】:passport don't redirect to 'failureRedirect' upon failure with facebook在使用 facebook 失败时,护照不会重定向到“failureRedirect”
【发布时间】:2019-03-08 01:34:24
【问题描述】:

我正在使用护照通过 Facebook 对用户进行身份验证。

successRedirect 效果很好 [我正在重定向到 http://localhost:3000/success#_=_],但 failureRedirect 没有,我明白了:

FacebookAuthorizationError: Login Error: There is an error in logging you into this application. Please try again later.
[I'm getting this in my browser-> http://localhost:3000/auth/facebook/callback?error_code=1349003&error_message=Login+Error%3A+There+is+an+error+in+logging+you+into+this+application.+Please+try+again+later.#_=_

这些是我的设置:

app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(function (user, done) {
    console.log(user);
    done(null, 'this is the user');
})

passport.deserializeUser(function (id, done) {
    console.log(id);
    done(err, {message: 'this is the user'});
});

router.get('/auth/facebook', passport.authenticate('facebook'));

router.get(
    '/auth/facebook/callback',
    passport.authenticate('facebook',
        {
            successRedirect: '/success',
            failureRedirect: '/login',
        }
    ),
);

const FacebookStrategy = require('passport-facebook').Strategy;

const facebookStrategy = new FacebookStrategy({
    clientID: staretegy.clientId,
    clientSecret: staretegy.clientSecret,
    callbackURL: staretegy.callbackURL,
    profileFields: [
        'id',
        'first_name',
        'middle_name',
        'last_name',
    ],
}, (accessToken, refreshToken, profile, done) => {
    done(null, {user: profile});
});

passport.use(facebookStrategy);

正如我在文档中看到的那样,我希望被重定向到 /login.

/login可以被浏览器访问。 (我也尝试过输入这个完整的 URL 路径:failureRedirect: http://localhost:3000/login,但它不起作用,类似的 URL 与 successRedirect 一起使用。

【问题讨论】:

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


    【解决方案1】:

    这似乎是一个open issue 并且几乎不支持主存储库。但是你可以尝试使用this fork

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题here,并找到了一种使用中间件错误处理程序来处理错误的方法(请参阅下面的 fbErrorHandler):

      const   express     = require('express'),
              router      = express.Router(),
              passport    = require('passport');
      
          router.get(
              '/facebook', 
              passport.authenticate('facebook')
          );
      
          function fbErrorHandler(err, req, res, next) {
              // I use flash, but use whatever you want to communicate with end-users:
              req.flash('error', 'Error while trying to login via Facebook: ' + err);
              res.redirect('/login');
          }
      
          router.get('/facebook/callback',
              passport.authenticate(
                  'facebook', 
                  { 
                      failureRedirect: '/login',
                      failureFlash: true
                  },
              ),
              fbErrorHandler,
              (req, res) => {
                  // Successful authentication
                  res.redirect('/authenticated');
              }
          );
      
          module.exports = router;
      

      【讨论】:

        猜你喜欢
        • 2017-09-27
        • 2021-04-16
        • 1970-01-01
        • 2019-01-19
        • 2018-03-19
        • 2013-05-27
        • 2016-04-16
        • 2022-08-04
        • 1970-01-01
        相关资源
        最近更新 更多