简体   繁体   中英

passportjs get authentication error message

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

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

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

I experienced many many problems and bugs and configurations issues during working with Passport. My solution was to move to Everyauth.

I don't know if this will be any use to you but I got access to flash messages this way. When you are defining the FacebookStrategy use the passReqToCallback parameter.

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

this will allow you to add the req.flash() as a parameter to the done() like so

return done(false, user, reg.flash('success', 'Sign in successfull'));

Hope that sheds some light on the situation for you or anybody else looking for help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM