user3060636
user3060636

Reputation: 272

req object in passport.authenticate

I am using passportjs for authentication on my server. 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? This was I can get the user ip address and check for eventual attacks.

Upvotes: 4

Views: 1276

Answers (1)

Akshat Jiwan Sharma
Akshat Jiwan Sharma

Reputation: 16000

The comments in the example suggest that you can pass an object { "passReqToCallback": true } to make the req callback available in the callback function. Which can be accessed as

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

So initialize passport.use as

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

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

and you should have req in the callback.

Upvotes: 2

Related Questions