【问题标题】:Node - Passport Auth - Authed Post Route hangs on form submission节点 - Passport Auth - Authed Post Route 在表单提交时挂起
【发布时间】:2013-07-24 07:51:34
【问题描述】:

这是一个奇怪的问题。 Im Passport 的“本地策略”适用于我的 express 应用程序,但我遇到了一个奇怪的问题。

基本上,我有 3 条路线。每个都有一个身份验证检查。

app.get('/admin', authenticatedOrNot, adminRoute.index);
app.get('/admin/new', authenticatedOrNot, adminRoute.newpost);
app.post('/admin/new', authenticatedOrNot, adminRoute.create);

authenticatedOrNot 方法很简单:

var authenticatedOrNot = function(req, res, next){
    if(req.isAuthenticated()){
        next();
    }else{
        res.redirect("/login");
    }
}

非常适合登录管理区域并检查用户是否已登录,但是当我将表单提交到“/admin/new”发布路径时,浏览器会挂起。即使使用了 console.log,控制台也没有任何反应:

exports.create = function(req, res){
    console.log(req);
        // Database logic here
        res.redirect('/admin');
}

我似乎无法让它工作。它只是挂起,并最终失败。浏览器控制台只是在网络请求中显示“待处理”。

我尝试从 post 路由和相同的问题中删除 'authenticatedOrNot' 方法,但如果我删除所有三个它就可以正常工作。

我被难住了。

有帮助吗?还有其他人遇到这个吗?

【问题讨论】:

  • 表格是否张贴到正确的路线?检查浏览器的开发工具以确认实际请求的路线正确。否则你的代码对我来说很好
  • 是的,我检查了网络。甚至改变了一点,以防我快疯了。好叫声。
  • 在您的发帖路线adminRoute.create 中,您确定要发回一些回复吗?您必须确定要结束响应。
  • 是的,我正在发送 res.redirect('/admin');但即使我将其更改为简单的 res.send(200),仍然存在问题。它与护照有关。它在没有身份验证的情况下完美运行,但被它破坏了。
  • 我发现 Express 和其他东西的配置顺序很重要。甚至您注册路线的顺序。试着摆弄它。

标签: node.js mongodb authentication express passport.js


【解决方案1】:

我遇到了与此非常相似的问题,因此我将其发布以防万一。 问题似乎是我在护照​​函数中有另一个函数定义,这阻止了完成处理程序被调用。我认为这是问题所在,因为当我更改函数参数名称时,事情就开始起作用了。

事后看来,我认为错误很明显,但由于我是 node 新手,我对函数、回调、闭包等仍然有点不确定。我也有这样的印象,即 node 约定总是使用这些参数名称(err、done、next)并且有一些与它们相关的魔法。我想不是。请随时就这一点对我进行教育。

无论如何,我使用的是从教程(http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local)中复制的护照本地策略。 本教程使用 mongo,但我决定切换到 postgresql。所以我使用了https://github.com/brianc/node-postgres-pure 的 pg.js 模块,并使用了提供的示例代码。

以下是代码的相关部分,在我最初将 pg.js 示例代码复制并粘贴到护照教程中之后:

//错误代码

passport.use('local', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { 
    pg.connect(configDB.connectionString, function(err, client, done) {
        if (err) {
            return console.error('could not connect to postgres', err);
        }
        client.query('select email, password_hash from admin_user where email = $1', [email], function(err, result) {

            // check password against db, and then try to call passports done callback
            return done(null, userModel); // this actually invokes the pg.connect done callback

        });
    });
}));

因此,当它运行时,在返回 /login 的帖子上,对 done 的调用将调用 pg.connect done,而不是护照完成。

// 好吗?工作代码

function(req, email, password, done) { 

    pg.connect(configDB.connectionString, function(err, client, connect_done) {
        if (err) {
            return console.error('could not connect to postgres', err);
        }
        client.query('select email, password_hash from admin_user where email = $1', [email], function(err, result) {
            connect_done() // free up postgres connection, which I should have been doing before
            // check password against db, and then
            return done(null, userModel); // invoke passport's done callback

        });
    });
}));

这段代码现在对我有用(除非我复制错误)。

【讨论】:

    【解决方案2】:

    当您拆分越来越多时,诊断此类问题变得更加容易...最好的方法是使用一些嗅探器(内置于 Chrome、Firefox、Opera 或独立)并准确获取您发送到的标头你的服务器。这非常有用,因为您可以将问题定位到前端应用程序(<form acton="/admin/new" - 例如错误输入)或后端。

    让我们向您道歉,您的标头没有问题,并且您在 /admin/new 路由上发送了准确的 POST。由于您的console.log( req ); 没有生效,显然应用程序没有达到这一点。这可能是因为 authenticatedOrNot 挂起或因为 adminRoute.create 未正确实例化。

    authenticatedOrNot 可能会挂在/login 重定向上,因为您没有提供处理此路由的方式。

    adminRoute.create 可能会导致一些麻烦,具体取决于您将其附加到应用中的方式。

    所以在简历中我需要查看更多代码来确定问题所在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 2014-01-04
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      相关资源
      最近更新 更多