本文介绍了Facebook失败后,护照不会重定向到"failureRedirect"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im using passport to authenticate a user through Facebook.

successRedirect的效果很好[我将重定向到http://localhost:3000/success#_=_],但是failureRedirect却不行,我得到了这个信息:

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

这些是我的设置

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.

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

/login可以由浏览器访问. (我也曾尝试放置完整的URL路径:failureRedirect: http://localhost:3000/login,但它不起作用,类似的URL在successRedirect下也适用.

/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.

推荐答案

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

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;

这篇关于Facebook失败后,护照不会重定向到"failureRedirect"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:05