gpt4 book ai didi

javascript - Passportjs 获取身份验证错误消息

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:22 25 4
gpt4 key购买 nike

我正在使用 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 身份验证失败,我如何获取错误消息(我想将其显示给用户)?

最佳答案

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

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

关于javascript - Passportjs 获取身份验证错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17058496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com