Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 快速通行证故障重定向不工作_Node.js_Authentication_Express_Passport.js - Fatal编程技术网

Node.js 快速通行证故障重定向不工作

Node.js 快速通行证故障重定向不工作,node.js,authentication,express,passport.js,Node.js,Authentication,Express,Passport.js,我设置了local策略,但failureRedirect似乎无法正常工作。当发生连接错误(例如,数据库的URL错误)时,响应为错误500,而不是重定向到指定的路由 这是我的路线代码: router.route('/login') .post(passport.authenticate('local', { failureRedirect: '/' }), function(req, res){ console.log('user logged

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

这是我的路线代码:

router.route('/login')
    .post(passport.authenticate('local', {
        failureRedirect: '/' 
    }), function(req, res){ 
        console.log('user logged in');
        res.redirect('../console');
    });
下面是我对
本地
策略的实施:

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()
回调函数是错误的,但我遵循了文档。谢谢你的帮助。

你必须

抛出新错误(“hello world”)

要触发故障重定向,您也可以尝试


我遇到的问题是,如果用户不存在,我就会抛出一个错误--
done('someerror',null)这似乎不是Passport所期望的

它支持这样一种观点,即虚假用户的行为是失败的另一个标志。因此,如果找不到用户,则相应的签名将是
done(null,null)


因此,当出现数据库错误时,请将“数据库错误”作为错误。您应该设置null。

谢谢,但没有按预期工作:它将错误打印到页面,而不是按照
failureRedirect
中的指定重定向到。酷,现在是什么错误?正如我所说,页面没有重定向到
failureRedirect
链接,而是停留在按钮
action
的路径上,在该路径上打印错误。完成参数是
done(err,user,options)
并且
options
可以获取消息参数。所以如果有异常,通过
err
传递,如果没有用户,将
user
返回为
false
,并设置选项,如
{message:“没有这样的用户”}
每当err不为null且user为false时,我仍然会遇到问题。如果err设置为null,则该选项有效<代码>完成(null,false,{消息:“没有这样的用户”})
工作正常。