节点.js护照不调用下一个函数
Node.js Passport not calling next function
我正在使用使用Passport的Node构建一个应用程序.js以处理使用本地数据库的用户登录。
所以我有以下代码,当用户转到/profile 时会被调用。成功登录后,用户将被重定向到/profile。根据摩根的说法,这确实发生了。
app.get('/profile', passport.authenticate('local-login', { session : false, failureRedirect : '/login' }), function(req, res) {
console.log("testnow");
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
我的本地登录代码如下。
passport.use('local-login', 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) { // callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
console.log("testdone");
return done(null, user);
});
}));
在测试代码时,我登录并在一瞬间被重定向到配置文件。控制台打印我的本地登录代码中的"testdone",但没有像预期的那样打印"testnow"。这意味着我的/profile get 方法中的第二个函数似乎永远不会被调用,即使本地登录正在调用下一个函数。
因此,从最终用户的角度来看,您登录(在幕后,您将被重定向到拆分部分的/profile),并且/profile 会将您重定向回/login。
关于如何解决此问题以便实际调用我的/profile get 方法中的第二个函数的任何想法?
提前非常感谢。我也很乐意提供任何其他信息来帮助解决这个问题。
passport.authenticate()
用于处理实际的身份验证;换句话说,获取登录凭据并将其传递给策略。如果请求已经过身份验证,则它并不意味着传递请求,而这正是您尝试使用它的目的。
相反,您希望使用类似 connect-ensure-login
的内容来保护用户必须登录的路由。
另请参阅此护照示例项目。
相关文章:
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- jQuery/Javascript-是否可以获得链中的下一个调用
- 在每个循环上完成异步调用,然后转到下一个节点 js
- 如何运行一个调用某个函数的函数首先完成它,然后再调用下一个函数.正在创建回调函数
- 在传递给下一个函数之前转义 Javascript 调用
- 快速:在错误处理程序中调用下一个
- 调用原型的重写方法,然后调用下一个原型的重写方法错误
- 反应 Redux 异步操作在完成函数后调用下一个函数
- 当滑块有多个实例时调用下一个上一个 iDangero.us
- 调用下一个函数jQuery Galleria插件后如何调用函数
- 如何创建一个函数来获取jquery中元素的下一个id并调用该函数
- 使用多个jquery.ajax()调用将一个下拉列表的值传递给另一个
- Jquery Queue请求集成一次First Request Success,然后调用下一个请求
- JavaScript链接承诺:在上一个承诺完成之前调用下一个承诺
- Q -在调用下一个操作之前等待操作完成
- ES.下一个装饰器可以直接调用,也可以作为工厂函数调用
- JavaScript没有按预期调用下一个函数
- 在调用下一个函数之前,如何等待一个函数完成呢
- 节点.js护照不调用下一个函数
- 在express get()中正确调用下一个回调