passport don't redirect to 'failureRedirect' upon failure with facebook

微笑、不失礼 提交于 2020-03-02 09:08:21

问题


Im using passport to authenticate a user through Facebook.

The successRedirect works great [I'm redirecting into http://localhost:3000/success#_=_], but the failureRedirect doesn't, i'm getting this:

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.#_=_

Those are my settings:

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

As i read in the docs i expected to be redirect to the /login.

/login can be accessed by the browser. (i've also tried to put this full URL path: failureRedirect: http://localhost:3000/login but it won't work, similar URL works with the successRedirect.


回答1:


This seems an open issue and the main repository barely supported. But you can try to use this fork.




回答2:


I had a similar issue here and have found a way to handle the error using a middleware error handler (see fbErrorHandler below):

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;


来源:https://stackoverflow.com/questions/52616560/passport-dont-redirect-to-failureredirect-upon-failure-with-facebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!