如何在passport-facebook/Passport.js中捕获FacebookAuthorizationError? [英] How to catch FacebookAuthorizationError in passport-facebook / Passport.js?

查看:267
本文介绍了如何在passport-facebook/Passport.js中捕获FacebookAuthorizationError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快递应用程序,该应用程序通过Passport.js和passport-facebook策略使用Facebook身份验证.我已经按照文档来处理身份验证.

I have an express app that uses Facebook authentication via Passport.js and the passport-facebook strategy. I have followed the documentation on how to handle authentication.

一切正常... 直到,Facebook尝试通过回调Url报告错误.在这种情况下,我收到了一个未捕获的FacebookAuthorizationError异常,并且未调用failRedirect.

It all works fine... until Facebook tries to report an error via the callback Url. In this case, I get an uncaught FacebookAuthorizationError exception, and the failureRedirect is not called.

由于未捕获的异常,最终用户看到内部服务器错误",我不遵守Facebook开发准则...

End users see an "Internal Server Error" as a result of the un-caught exception and I am in non-compliance with Facebook development guidelines...

http://localhost:8081/auth/facebook/callback?error_code=1340031&error_message=Unable+to+Log+In%3A+There+is+a+60-minute+delay+before+new+accounts+can+log+in+to+any+applications.+Please+try+again+in+an+hour.#_=_

异常(被截断):

FacebookAuthorizationError: Unable to Log In: There is a 60-minute delay before new accounts can log in to any applications. Please try again in an hour.
    at Strategy.authenticate (/Users/Dan/project/node_modules/passport-facebook/lib/strategy.js:81:23)
    at attempt (/Users/Dan/project/node_modules/passport/lib/middleware/authenticate.js:361:16)
    at authenticate (/Users/Dan/project/node_modules/passport/lib/middleware/authenticate.js:362:7)
    at Layer.handle [as handle_request] (/Users/Dan/project/node_modules/express/lib/router/layer.js:95:5)

我阅读了较旧的文章( passport not在Facebook失败时重定向到"failureRedirect" ),这可能是一个未解决的问题.

I have read an older post (passport don't redirect to 'failureRedirect' upon failure with facebook) that it could be an open issue.

我认为这个护照插件将非常受欢迎,并且有人可以解决-有人知道吗?

I would think this passport plugin would be extremely popular, and that someone would have a workaround - does anyone know?

非常感谢.

function initExpress() {
    const express                 = require('express'),
        passport                = require('passport'),
        LocalStrategy           = require('passport-local'),
        FacebookStrategy        = require('passport-facebook').Strategy,
        expressSession          = require('express-session');
    }
    let app             = express();

app.use(expressSession(
    {
        secret:             secret,
        resave:             false,
        saveUninitialized:  false
    }
));
app.use(flash());

passport.use(new LocalStrategy(User.authenticate()));

passport.use(new FacebookStrategy({
    clientID:       process.env.FACEBOOK_APP_ID, 
    clientSecret:   process.env.FACEBOOK_APP_SECRET, 
    callbackURL:    `${process.env.SERVER_API_URL}/auth/facebook/callback`,
    profileFields:  ['id', 'displayName', 'name', 'picture']
    },
    function(accessToken, refreshToken, profile, done) {
        facebookFindOrCreateUser(accessToken, refreshToken, profile, done);
}
));

app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

 etc..
}

/auth/路由:

const   express     = require('express'),
    router      = express.Router(),
    passport    = require('passport');

router.get(
    '/facebook', 
    passport.authenticate('facebook')
);

router.get('/facebook/callback',
    passport.authenticate(
        'facebook', 
        { 
            failureRedirect: '/login',
            failureFlash: true
        }
    ),
    (req, res) => {
        // Successful authentication
        res.redirect('/authenticated');
    }
);

module.exports = router;

推荐答案

好,我制定了一个解决方案-通过实现中间件错误处理程序.希望这对某人有用-因为我在passport-facebook文档中找不到有关此信息的任何信息.

Ok, I worked out a fix - via implementing a middleware error handler. Hope this is useful to someone - as I couldn't find any info about it in the passport-facebook documentation.

它捕获以前未捕获的异常,并防止Express向最终用户提供内部服务器错误".

It catches the previously un-caught exception and prevents the "Internal Server Error" that Express was giving to end users.

const   express     = require('express'),
        router      = express.Router(),
        passport    = require('passport');

    router.get(
        '/facebook', 
        passport.authenticate('facebook')
    );


    function fbErrorHandler(err, req, res, next) {
        // I use flash, but use whatever notification method you want for end users:
        req.flash('error', 'Error while trying to login via Facebook: ' + err);
        res.redirect('/login');
    }

    router.get('/facebook/callback',
        passport.authenticate(
            'facebook', 
            { 
                failureRedirect: '/login',
                failureFlash: true
            },
        ),
        fbErrorHandler,
        (req, res) => {
            // Successful authentication
            res.redirect('/authenticated');
        }
    );

    module.exports = router;

这篇关于如何在passport-facebook/Passport.js中捕获FacebookAuthorizationError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆