简体   繁体   English

Express Passport failureRedirect无法正常工作

[英]Express Passport failureRedirect not working

I set up the local strategy but failureRedirect does not seem to work properly. 我设置了local策略,但failureRedirect似乎无法正常工作。 When a connection error occurs (for instance the URL of the database is wrong), the response is an error 500 instead of redirect to the specified route. 发生连接错误时(例如,数据库的URL错误),响应是错误500而不是重定向到指定的路由。

This is my route code: 这是我的路线代码:

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

And here is my implementation for the local strategy: 这是我对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);
                            }
                        });
                }
            });
        }
    ));
};

I was thinking maybe my use of done() callback function is wrong but I followed the documentation. 我想也许我使用done()回调函数是错误的,但我按照文档。 Thanks for your help. 谢谢你的帮助。

The issue I had was that I was throwing an err if the user didn't exist -- done('some error', null); 我遇到的问题是,如果用户不存在,我会抛出一个错误 - done('some error', null); and that doesn't seem to be what Passport expects. 而这似乎并不像Passport所期望的那样。

It supports the notion of a falsy user in done as another sign of failure. 它支持虚假用户的概念作为失败的另一个标志。 So the appropriate signature would be done(null, null) if you don't find a user. 因此done(null, null)如果找不到用户done(null, null)done(null, null)相应的签名done(null, null)

so when there is a database error you put 'Error with database' as the error. 因此,当出现数据库错误时,将“数据库错误”作为错误。 you should put null. 你应该把null。

You have to 你必须

throw new Error('hello world') 抛出新的错误('你好世界')

to trigger failure Redirect, you may also try 触发失败重定向,你也可以试试

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

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

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