繁体   English   中英

Express Passport failureRedirect无法正常工作

[英]Express Passport failureRedirect not working

我设置了local策略,但failureRedirect似乎无法正常工作。 发生连接错误时(例如,数据库的URL错误),响应是错误500而不是重定向到指定的路由。

这是我的路线代码:

router.route('/login')
    .post(passport.authenticate('local', {
        failureRedirect: '/' 
    }), function(req, res){ 
        console.log('user logged in');
        res.redirect('../console');
    });

这是我对local战略的实施:

module.exports = function(){
    passport.use(new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password'
    },
        function(email, password, done){
            pg.defaults.ssl = true;
            pg.connect(process.env.DATABASE_URL, function(err, client) {
                if (err){
                    console.log('Connection issue when logging in: ' + JSON.stringify(err));
                    done('Error with database,', null); // this is the problem area!!!
                } else {
                    client
                        .query(`SELECT * FROM agent WHERE email='${email}'`, function(err, result) {
                            if(err || result.rows.length === 0 ) {
                                console.log('Query issue when loggin in: '+ JSON.stringify(err));
                                done(null, false);
                            } else {
                                var user = result;
                                console.log('ready to log user in');
                                done(null, user);
                            }
                        });
                }
            });
        }
    ));
};

我想也许我使用done()回调函数是错误的,但我按照文档。 谢谢你的帮助。

我遇到的问题是,如果用户不存在,我会抛出一个错误 - done('some error', null); 而这似乎并不像Passport所期望的那样。

它支持虚假用户的概念作为失败的另一个标志。 因此done(null, null)如果找不到用户done(null, null)done(null, null)相应的签名done(null, null)

因此,当出现数据库错误时,将“数据库错误”作为错误。 你应该把null。

你必须

抛出新的错误('你好世界')

触发失败重定向,你也可以试试

https://docs.nodejitsu.com/articles/errors/what-is-try-catch/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM