NodeJS jwtStrategy requires a function to retrieve jwt from requests error
From the official documentation, when migrating from 2.x to 3.x using JWT you should use:
ExtractJwt.fromAuthHeaderWithScheme('jwt')
instead of the old one:
ExtractJwt.fromAuthHeader()
I think you are using 'passport-jwt' 2.0.0 which has added some breaking changes from v1.x.x used by the tutorial. In the opts
you need to pass another option jwtFromRequest
to tell it where to look for jwt payload.
var JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;
var opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
opts.secretOrKey = config.secret;
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
User.findOne({id: jwt_payload.id}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false);
// or you could create a new account
}
});
}));