1

I am using passportjs for facebook authentication. Here is my facebook strategy:

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

I added the following routes:

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);

My users.authFailure method is:

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'
    });
};

In case of facebook authentication failure, how can I get the error message (I want to display it to the user)?

4

3 回答 3

0

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

于 2013-06-13T06:43:36.610 回答
0

我不知道这对您是否有用,但我可以通过这种方式访问​​ 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', '登录成功'));

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

于 2014-06-19T21:05:09.000 回答
0

由于您正在使用failureFlash,因此应该这样做:

var error = req.flash('error');
于 2013-06-12T06:04:52.713 回答