cholalabs / passport-localapikey

MIT License
165 stars 57 forks source link

How to access req.host in passport.js? #3

Open jetsonjohn opened 10 years ago

jetsonjohn commented 10 years ago

I am using passportjs library for authenticating the user with api key. What I am trying to do is that along with api key. I also want to check the host name of the request.

app.post('/api/authenticate', 
  passport.authenticate('localapikey'),
  function(req, res) {
    console.log('Authenticated');
  }); 

I don't know how passportjs calling the below function. But it definitely calling the function after a post request is coming to the path '/api/authenticate'. I also want to access the req.host in the below function.

passport.use(new LocalStrategy(
  function(apikey, done) {
    console.log(req.host);
}

Is it possible? Any insight into this would highly be appreciated. Thank you

danielgratzl commented 10 years ago

The Strategy has an option "passReqToCallback". If this is set to "true" the verify callback will be called with an instance of the request.

passport.use(new LocalStrategy(
  { passReqToCallback: true },
  function(req, apikey, done) {
    console.log(req.host);
}