如何处理npm护照-facebook模块中的错误

时间:2018-12-30 20:19:08

标签: node.js passport-facebook

我正在尝试通过password-facebook模块在node.js中实现Facebook登录。

我的问题是我如何正确地捕获验证回调函数cb()引发的错误。以及如何将该错误传递到/ facbook / fail页面以显示有关此消息的友好消息?

// passport init
passport.use(new FacebookStrategy({
        clientID: api.CONFIG.fbAuth.clientID,
        clientSecret: api.CONFIG.fbAuth.clientSecret,
        callbackURL: null,
        profileFields: ['id', 'emails', 'name', "birthday", "gender"]
    },
    function(accessToken, refreshToken, profile, cb) {
        return cb("Throw some error"); // throw some error here for testing porpuses
    }
));

// routes
router.get('/facebook', function(req, res, next) {
    passport.authenticate("facebook", {
        scope : ["email", "user_birthday", "user_gender"],
        callbackURL: "callbackUrl"
    })(req, res, next);
});

router.get('/facebook/callback', 
    function(req, res, next) {
        passport.authenticate("facebook", {
            callbackURL: "callbackUrl",
            failureRedirect: "failUrl"
        })(req, res, next);
    }, 
    function(req, res) {
        res.send("Facebook authentication successful.");
    }
);

router.all("/facebook/fail", failCallback(req, res) {
    // get info about the error thrown by cb() function
    let error = req.error;
    res.send("Facebook authentication failed.");
});

0 个答案:

没有答案