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....
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) => {
...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With