使用PassportJS,如何将额外的表单字段传递给本地认证策略?

108

我正在使用passportJS,想要向我的认证策略(passport-local)提供不止req.body.usernamereq.body.password,而是想要提供更多内容。

我有3个表单字段:usernamepasswordfoo

我该如何从我的本地策略中访问req.body.foo,该策略看起来像:

passport.use(new LocalStrategy(
  {usernameField: 'email'},
    function(email, password, done) {
      User.findOne({ email: email }, function(err, user) {
        if (err) { return done(err); }
        if (!user) {
          return done(null, false, { message: 'Unknown user' });
        }
        if (password != 1212) {
          return done(null, false, { message: 'Invalid password' });
        }
        console.log('I just wanna see foo! ' + req.body.foo); // this fails!
        return done(null, user, aToken);

      });
    }
));
我将在路由内调用此函数(而非作为路由中间件),如下所示:
  app.post('/api/auth', function(req, res, next) {
    passport.authenticate('local', {session:false}, function(err, user, token_record) {
      if (err) { return next(err) }
      res.json({access_token:token_record.access_token});
   })(req, res, next);

  });
2个回答

197

可以启用 passReqToCallback 选项,例如:

passport.use(new LocalStrategy(
  {usernameField: 'email', passReqToCallback: true},
  function(req, email, password, done) {
    // now you can check req.body.foo
  }
));

req 成为验证回调函数的第一个参数时,您可以自行检查它。


9
非常感谢,这很好用。指南中有关于passReqToCallback的内容吗?我没有看到。 - k00k
2
还没有。我在添加新功能/选项到指南上有些落后。 - Jared Hanson
9
这对于实现多租户认证非常有帮助,但在我的谷歌搜索中没有找到相关内容。希望我的评论能够帮助其他人在未来找到这个答案。 - Kris Dahl
这非常有帮助。但是是否可以根据条件设置usernameField?我有两个选项:一个是电子邮件,另一个是电话号码。登录表包含这两个字段以及密码。 - Mathew John
非常有用。对于任何想知道的人,passReqToCallback也存在于passport-http模块中。基于此,我猜它也在许多其他策略中。 - wednesdaymiko
我已经尝试了相同的方法,但仍然收到缺少凭据的错误提示。@MathewJohn - Rupali Pemare

1
在大多数情况下,我们需要提供两个登录选项:
  • 使用电子邮件
  • 使用手机号
很简单,我们可以采用常见的用户名字段并通过两个选项查询$or,我发布了以下代码片段,如果有人有相同的问题。
我们还可以使用'passReqToCallback'作为最佳选项,感谢@Jared Hanson。
passport.use(new LocalStrategy({
    usernameField: 'username', passReqToCallback: true
}, async (req, username, password, done) => {
    try {
        //find user with email or mobile
        const user = await Users.findOne({ $or: [{ email: username }, { mobile: username }] });

        //if not handle it
        if (!user) {
            return done(null, {
                status: false,
                message: "That e-mail address or mobile doesn't have an associated user account. Are you sure you've registered?"
            });
        }

        //match password
        const isMatch = await user.isValidPassword(password);
        debugger
        if (!isMatch) {
            return done(null, {
                status: false,
                message: "Invalid username and password."
            })
        }

        //otherwise return user
        done(null, {
            status: true,
            data: user
        });
    } catch (error) {
        done(error, {
            status: false,
            message: error
        });
    }
}));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接