'Passport authenticate access to req

I have set up azureAD login with passport in my Express-server. I would like to access the req object in the passport.authenticate middleware, is this possible somehow?

this.router.post(
    'login/azure/return',
    passport.authenticate('azuread-openidconnect', {
        session: false,
        failureRedirect: '' // I would like access to the req object HERE
    }),
    (req, res) => {
        ...
    }


Solution 1:[1]

You can't use the req object because these are default options provided based on the authentication or the configuration you want to provide to a user. For sample you can check this:

passport.authenticate('azuread-openidconnect', { failureRedirect: '/', session: false, customState: 'my_state', resourceURL: 'https://graph.microsoft.com/mail.send'}, (req, res, next) => {
    log.info('Login was called in the Sample');
    res.redirect('/');
});

For more information, you can check the details here.

Solution 2:[2]

Yes, it is possible.

Look at your code, at some point you have to define your OIDCStrategy, which looks something like this:

var strategy = new OIDCStrategy({
  "clientID": "clientId goes here",
  // a bunch of attributes will be listed here
  "passReqToCallback": true   //  <--- this will do the trick
  }, 
  function authenticationCallback(req, iss, sub, profile, accessToken, refreshToken, done){  
     //  you will get "req" in this call back
    

  }
);

When defining the options of your strategy make sure you use the attribute "passReqToCallback": true (as shown in the example), and use the function signature also provided in the example, the first parameter will be your "req" object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Apoorva Chikara
Solution 2 Victor Ayala