节点 – Passport Auth – Authed Post路由在表单提交时挂起

这是一个奇怪的。 我护照的“本地策略”为我的快递应用程序,我遇到了一个奇怪的问题。

本质上,我有三条路线。 每个人都有一个auth检查到位。

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"); } } 

完美的login到pipe理区域,并检查用户是否login,但是当我提交表单到'/ admin / new'Postpath时,浏览器挂起。 控制台中没有任何事情发生,即使使用console.log也是如此:

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

我似乎无法得到它的工作。 它只是挂起,最终失败。 浏览器控制台只是说在networking请求中的“挂起”。

我曾尝试从postpath和相同的问题中删除“authenticatedOrNot”方法,但如果我删除所有三个,它工作正常。

我难倒了。

任何帮助家伙? 其他人遇到这个?

我有一个非常相似的问题,所以我张贴这个以防万一。 这个问题似乎是,我在护照function里面有另一个函数定义,这阻止了调用完成的处理程序。 我认为这是问题,因为当我改变函数参数名称的东西开始工作。

事后看来,我认为这个错误是显而易见的,但是由于我是新手,对于函数,callback,闭包等我还是有点不确定。我也有这样的印象:节点约定总是使用这些参数名字(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,而不是passport 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 }); }); })); 

这段代码现在正在为我工​​作(除非我错误地复制了一些东西)。

当你分裂得越来越多时,诊断这样的麻烦变得越来越容易了。最好的办法是使用一些嗅探器(内置在Chrome,Firefox,Opera或者独立版本中),并且准确地获得你发送到服务器的头文件。 这是非常有用的,因为你可以将问题本地化到前端应用程序( <form acton="/admin/new" – 例如input错误)或后端。

让我们道歉你的标题是好的,你发送完全POST在/admin/new路线。 由于你的console.log( req ); 不起作用明显应用不到这一点。 这可能是因为authenticatedOrNot挂起或因为adminRoute.create未正确实例化。

authenticatedOrNot可能挂在/loginredirect,因为我看到,因为你没有提供如何处理这条路线的方式。

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

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