这是一个奇怪的问题。Im Passport的‘本地策略’为我的express应用程序,我遇到了一个奇怪的问题。
从本质上讲,我有三条路线。每一个都有一个身份验证检查。
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”Post路径提交表单时,浏览器挂起。控制台中什么都不会发生,即使console.log已经就位:
exports.create = function(req, res){
console.log(req);
// Database logic here
res.redirect('/admin');
}
我好像不能让它工作。它只是挂起,并最终失败。浏览器控制台只是在网络请求中显示'pending‘。
我已经尝试从post路由中删除'authenticatedOrNot‘方法,但如果我将所有三个都删除,它工作得很好。
我被难住了。
有帮手吗?还有别的人碰到这个吗?
发布于 2014-03-03 16:27:30
我有一个与此非常相似的问题,所以我把这篇文章贴出来,以防它能有所帮助。问题似乎是我在passport函数中有另一个函数定义,这阻止了done处理程序被调用。我认为这就是问题所在,因为当我更改函数参数名称时,它就开始工作了。
事后看来,我认为错误是显而易见的,但由于我是第一次接触node,我对函数、回调、闭包等仍然有点不确定。我也有这样的印象,即节点约定总是使用这些参数名称(err、done、next),并且与它们相关的有一些魔力。我想不是这样的。在这一点上,请随时教我。
无论如何,我使用的是从教程(在http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local)复制的passport本地策略。本教程使用的是mongo,但我决定切换到postgresql。因此,我使用了来自https://github.com/brianc/node-postgres-pure的pg.js模块,并使用了提供的示例代码。
在我最初将pg.js示例代码复制并粘贴到passport教程之后,下面是代码的相关部分:
//错误代码
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的post中,对done的调用将调用passport 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
});
});
}));
这段代码现在可以为我工作了(除非我错误地复制了一些东西)。
发布于 2014-03-03 06:59:31
当你拆分得越来越多时,诊断这样的麻烦变得容易得多.最好的方法是使用一些嗅探器(内置在Chrome、Firefox、Opera或独立浏览器中),并准确地获取您发送到服务器的头文件。这非常有用,因为您可以将问题定位到前端应用程序(例如,<form acton="/admin/new"
错误类型)或后端。
让我们向您道歉,您的标题没有问题,并且您在/admin/new
路由上准确地发送了POST。由于您的console.log( req );
不会生效,因此应用程序显然不会到这一步。这可能是因为authenticatedOrNot
挂起或因为adminRoute.create
未正确实例化。
正如我所见,authenticatedOrNot
可能会挂起/login
重定向,因为您没有提供如何处理此路由的方法。
adminRoute.create
可能会带来一些麻烦,这取决于你将它附加到你的应用程序中的方式。
因此,在简历中,我需要看到更多你的代码来建立麻烦。
https://stackoverflow.com/questions/17840762
复制相似问题