【问题标题】:req (request) is not defined for passportjs.use in SailsJS在 SailsJS 中没有为 passportjs.use 定义 req(请求)
【发布时间】:2014-11-06 06:19:12
【问题描述】:

我想使用 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 吗?我对express和sailsjs还很陌生,请原谅我的无知。

【问题讨论】:

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


    【解决方案1】:

    对于sails v0.10.x sails-generate-auth 非常适合。

    【讨论】:

    • 感谢您的建议。我已经尝试过了,但它没有我需要的东西。 (护照-http)。
    • 其实也支持passport-http...我用的是这个github.com/jaredhanson/passport-http-bearer编辑:不知道passport-http是不是和passport-http-bearer类似...跨度>
    • 没有,我建议作者制作一个。但在那之前我不能真正使用它。而且我正在使用相当定制的护照版本,需要在回调时进行更新。我还不如自己定制护照。 (:
    • 你可以调整sails-generate-auth的services/passport.js ..我可以看到req param那里...
    猜你喜欢
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多