【问题标题】:passportjs get authentication error messagepassportjs 获取身份验证错误消息
【发布时间】:2013-06-08 03:38:57
【问题描述】:

我正在使用 passportjs 进行 Facebook 身份验证。这是我的 Facebook 策略:

passport.use(new FacebookStrategy({
        clientID: config.facebook.clientID,
        clientSecret: config.facebook.clientSecret,
        callbackURL: config.facebook.callbackURL
    }, function(accessToken, refreshToken, profile, done) {
        User.findOne({ 'facebook.id': profile.id }, function (err, user) {
            if (err) { return done(err); }
            if (!user) {
                user = new User({
                    name: profile.displayName,
                    email: profile.emails[0].value,
                    username: profile.username,
                    provider: 'facebook',
                    facebook: profile._json
                });
                user.save(function (err) {
                    if (err) {
                        console.log(err);
                    }
                    return done(err, user);
                });
            } else {
                return done(err, user);
            }
        });
    }));

我添加了以下路线:

app.get('/facebook/auth', passport.authenticate('facebook', { scope: [ 'email', 'user_about_me', 'publish_actions']}), function(req, res) { });

// I need the following fix due to this: http://stackoverflow.com/a/17015836/289246
app.get('/facebook/auth/callback', function(req, res, next) {
        if (req.query && !req.query.error && req.query.error_code) {
            req.query.error = true;
        }
        next();
    },
    passport.authenticate('facebook', { failureRedirect: '/facebook-auth-failure', successRedirect: '/auth-success', failureFlash: true })
);

app.get('/facebook-auth-failure', users.authFailure);
app.get('/auth-success', users.authSuccess);

我的 users.authFailure 方法是:

exports.authFailure = function (req, res) {
    var error = ??? 
    // How can I get here the error message??
    res.render('auth-failure', {
        error: error || 'An error has accured'
    });
};

如果 facebook 验证失败,我如何获得错误信息(我想显示给用户)?

【问题讨论】:

    标签: javascript node.js express passport.js


    【解决方案1】:

    由于您使用的是failureFlash,因此应该这样做:

    var error = req.flash('error');
    

    【讨论】:

    • 当我将 console.log(req.flash('error')); 添加到 authFailure 处理程序时,它会打印我 [] 而不是错误。有什么想法吗?
    • @Naor flash 消息存储在用户的会话中,也许您的会话过期太快了?调试 Passport 的一个好方法是使用自定义回调,如 on this page 所述(搜索“自定义回调”)
    【解决方案2】:

    在使用 Passport 期间,我遇到了许多问题、错误和配置问题。我的解决方案是迁移到 Everyauth。

    【讨论】:

    • 太糟糕了,我在几个项目中都使用过 Passport,它对我来说一直很好:)
    • @robertklep:你在 facebook 上用过吗?我要求不高,只是简单的登录,而且失败了不止一次。
    • 不是 Facebook,但我已经很好地使用了 Google 和本地策略。我不得不承认,在一切变得有意义之前,你必须“获得”护照,everyauth 看起来也很成熟 :)
    【解决方案3】:

    我不知道这对您是否有用,但我可以通过这种方式访问​​ Flash 消息。 当您定义 FacebookStrategy 时,请使用 passReqToCallback 参数。

    passport.use(new FacebookStrategy({
       clientID: facebook.getClientID(),
       clientSecret: facebook.getClientSecret(),
       callbackURL: facebook.getCallback(),
       passReqToCallback: true
    

    这将允许您像这样将 req.flash() 作为参数添加到 done() 中

    return done(false, user, reg.flash('success', '登录成功'));

    希望能为您或其他寻求帮助的人提供一些线索

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2020-08-22
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2018-10-11
      • 2012-05-09
      相关资源
      最近更新 更多