【问题标题】:Passport authentiation middleware res not found未找到护照身份验证中间件资源
【发布时间】:2018-06-06 04:48:52
【问题描述】:

我有以下代码库

passport.use(new LocalStrategy({
  usernameField: 'emailAddress',
  passwordField: 'password',
  passReqToCallback: true
},
 function(req,username, password, next) {
   var res =""; // this is the real issue, I don't know where to find res, or what should I initialize to re
   console.log('psprt val');
   userController.ValidateLogin(req,res,function(userDetails){
      req.user = userDetails;
      return next(null,userDetails);
    });
 }));

userController.ValidateLogin 以类似

的格式返回错误
userController.ValidateLogin = function(req,res,userDetails) {
    return res.send({ Status:403, Msg: "Not allowed" });
}

但它说 res.send 不是函数,因为有 res undefined ,如何发送 res 以便我有这种消息作为回报

【问题讨论】:

  • 告诉我们有问题的代码行return res.send({ Status:403, Msg: "Not allowed" });在哪里。向我们展示usercontroller.ValidateLogin() 的代码。这就是问题所在。
  • 这是来自验证登录功能
  • 是的,给我们看一下那个函数的代码吧!
  • 粘贴ValidateLogin()函数的代码。
  • 你可以看到我在上面给出了 res=' ',因为我没有 res

标签: node.js express passport.js middleware passport-local


【解决方案1】:

您不能从配置中间件发送响应(只能用于我们验证用户的护照配置)。您需要对用户进行身份验证和验证并返回,以下代码可能会对您有所帮助,

userController.ValidateLogin = function(req,res,callback) {
    return callback({ Status:403, Msg: "Not allowed" });
}

【讨论】:

  • @jfriend00 从中间件发送响应违背了使用中间件的目的。
  • @jfriend00 我们可以从中间件发送响应,但我指的是提供的中间件,我们无法发送响应,您可以参考文档
  • 他们来自这类任务(比如验证用户)。
  • @ArifKhan 如何将其作为中间件的响应发送
  • 当请求进入中间件时,检查用户是否有效,如果无效则立即返回响应。
猜你喜欢
  • 2019-06-14
  • 2018-11-06
  • 2021-07-05
  • 2017-01-06
  • 2019-02-28
  • 2018-05-02
  • 2017-06-14
  • 1970-01-01
  • 2017-04-24
相关资源
最近更新 更多