Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport JWT Strategy extracting options

Using Passport JWT Strategy, I'm passing the token down via params, and extracting the token like this ExtractJWT.fromUrlQueryParameter('secret_token').

But sometimes I'm passing the token down via header, I would like to extract it like this ExtractJWT.fromHeader('secret_token').

How can I check how its being passed down and use the correct extracting method dynamically.

This is my code:

passport.use(new JWTstrategy({
  secretOrKey: process.env.AUTH_SECRET,
  jwtFromRequest: ExtractJWT.fromUrlQueryParameter('secret_token')

}, async (token, done) => {
  try {
    //Pass the user details to the next middleware
    return done(null, token.user);
  } catch (error) {
    done(error);
  }
}));

Thank you! Im on this for a long time....

like image 915
Morris S Avatar asked Oct 11 '25 13:10

Morris S


1 Answers

Use ExtractJwt.fromExtractors() method

var jwtStrategy = new JwtStrategy({
// this will try to extract from Query parm, header and Authheader
  jwtFromRequest: ExtractJwt.fromExtractors([ExtractJwt.fromUrlQueryParameter("secret_token"), ExtractJwt.fromHeader("secret_token"), ExtractJwt.fromAuthHeaderAsBearerToken()]),
//here we have defined all possible extractors in an array
  secretOrKey: process.env.AUTH_SECRET
}, async (payload, done) => {
  ...
});
like image 182
Binu Balan Avatar answered Oct 14 '25 10:10

Binu Balan