在SailsJS中没有为passportjs.use定义req(请求)



我想使用req.flash("message":error)来支持SailsJS内部passportJS的错误消息回调。然而,PassportJS在回调期间不处理以下问题:(类似的帖子:PassportJS自定义身份验证回调未调用)

//there is no req found
req.flash("message" , "invalid password");

如果有这样的东西,这通常是好的:

function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
  //....
  req.flash("message" , "invalid password");
}

但我不能在passport.use内使用。

/services/passport.js
passport.use(new HttpBasicStrategy(
  function(username, password, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {
      // Find the user by username.  If there is no user with the given
      // username, or the password is not correct, set the user to `false` to
      // indicate failure.  Otherwise, return the authenticated `user`.
      findByUsername(username, function(err, user) {
        if (err)
          return done(null, err);
        if (!user) {
          return done(null, false, {
            message: 'Unknown user ' + username
          });
        }
        bcrypt.compare(password, user.password, function (err, res) {
          if (!res){
       -->  //there is no req found
       -->  req.flash("message" , "invalid password");
            return done(null, false, {
              message: 'Invalid Password'
            });
          }
          var returnUser = {
            username: user.username,
            createdAt: user.createdAt,
            id: user.id
          };
          return done(null, returnUser, {
            message: 'Logged In Successfully'
          });
        });
      })
    });
  }
));

有其他方法可以调用req.flash吗?我对快递很陌生,请原谅我的无知。

对于sails v0.10.x,生成auth非常适合。

最新更新