简体   繁体   English

passport.authenticate中的req对象

[英]req object in passport.authenticate

I am using passportjs for authentication on my server. 我在我的服务器上使用passportjs进行身份验证。 I am using the following code: 我使用以下代码:

exports.auth = function(req, res, next){
  passport.authenticate('bearer', { session: false })(req, res, next);
};

passport.use(new BearerStrategy(
  function(token, done) {
    User.findOne({ token: token }, function (err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false);
      }
      return done(null, user, { scope: 'read' });
    });
  }
));

Is there a way to access the req object in passport.use? 有没有办法访问passport.use中的req对象? This was I can get the user ip address and check for eventual attacks. 这是我可以获取用户的IP地址并检查最终的攻击。

The comments in the example suggest that you can pass an object { "passReqToCallback": true } to make the req callback available in the callback function. 示例中的注释表明您可以传递一个对象{ "passReqToCallback": true }以使回调函数中的req回调可用。 Which can be accessed as 哪个可以访问

function(req, token, done){//rest of the function body}

So initialize passport.use as 所以初始化passport.use as

passport.use(new BearerStrategy({ "passReqToCallback": true },

 function(req, token, done) {
  });  

and you should have req in the callback. 你应该在回调中有req。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM