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

时间:2014-11-06 06:19:12

标签: express request response sails.js passport.js

我想使用req.flash(“message”:error)来支持SailsJS中passportJS的错误消息回调。但是,PassportJS在回调期间不处理以下内容:(类似帖子:PassportJS Custom Authenticate Callback Not Called

//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吗?我是快递和sailsjs的新手,请原谅我的无知。

1 个答案:

答案 0 :(得分:1)

对于风帆v0.10.x sails-generate-auth是完美的。