Facebook失败后,护照不会重定向到“ failureRedirect”

时间:2018-10-02 21:20:13

标签: node.js passport.js passport-facebook

我正在使用护照通过Facebook验证用户身份。

successRedirect很好用[我将重定向到http://localhost:3000/success#_=_],但是failureRedirect却不行,我得到了这个提示:

FacebookAuthorizationError: Login Error: There is an error in logging you into this application. Please try again later.
[I'm getting this in my browser-> http://localhost:3000/auth/facebook/callback?error_code=1349003&error_message=Login+Error%3A+There+is+an+error+in+logging+you+into+this+application.+Please+try+again+later.#_=_

这些是我的设置

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

passport.serializeUser(function (user, done) {
    console.log(user);
    done(null, 'this is the user');
})

passport.deserializeUser(function (id, done) {
    console.log(id);
    done(err, {message: 'this is the user'});
});

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

router.get(
    '/auth/facebook/callback',
    passport.authenticate('facebook',
        {
            successRedirect: '/success',
            failureRedirect: '/login',
        }
    ),
);

const FacebookStrategy = require('passport-facebook').Strategy;

const facebookStrategy = new FacebookStrategy({
    clientID: staretegy.clientId,
    clientSecret: staretegy.clientSecret,
    callbackURL: staretegy.callbackURL,
    profileFields: [
        'id',
        'first_name',
        'middle_name',
        'last_name',
    ],
}, (accessToken, refreshToken, profile, done) => {
    done(null, {user: profile});
});

passport.use(facebookStrategy);

在我阅读文档时,我希望将其重定向到/login.

/login可以被浏览器访问。 (我也曾尝试输入完整的URL路径:failureRedirect: http://localhost:3000/login,但它不起作用,类似的URL可以与successRedirect一起使用。

2 个答案:

答案 0 :(得分:0)

这似乎是open issue,几乎不支持主存储库。但是您可以尝试使用this fork

答案 1 :(得分:0)

我遇到了类似的问题here,并且找到了一种使用中间件错误处理程序处理错误的方法(请参见下面的fbErrorHandler):

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 you want to communicate with 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;