0
votes

I'm getting this weird error authenticating with Password-Jwt, i'm kinda new to this so any help would be much appreciated. passport.js file :

         const jwtStrategy = require('passport-jwt').Strategy;
         const extractJwt = require('passport-jwt').ExtractJwt;
         const User = require('../models/user');
         const config = require('../config/database');

         module.exports = function(passport){ 
         let opts = {};
         opts.jwtFromRequest = 
         extractJwt.fromAuthHeaderAsBearerToken();
         opts.secretOrKey = config.secret;
         passport.user(new jwtStrategy, opts, function(jwt_payload, 
         done){
         User.getUserById(jwt_payload._id, function(err, user){
         if(err){
            return done(err, false);
        }
        if(user){
            return done(null, user);
        }else{
            return done(null, false);
        }
        });
     });
     }

I'm getting this error :

this._secretOrKeyProvider = options.secretOrKeyProvider; ^

TypeError: Cannot read property 'secretOrKeyProvider' of undefined at new JwtStrategy (/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/node_modules/passport-jwt/lib/strategy.js:33:41) at module.exports (/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/config/passport.js:10:19) at Object. (/home/mostfaubuntu/Desktop/desktop/MEAN_APP_PROJECT/app.js:34:29) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) at startup (bootstrap_node.js:188:16)

1

1 Answers

0
votes

So you have to use new JwtStrategy(opts, callbackFunction)

Have a look at the sample usage from the documentation.

passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
    User.findOne({id: jwt_payload.sub}, function(err, user) {
        if (err) {
            return done(err, false);
        }
        if (user) {
            return done(null, user);
        } else {
            return done(null, false);
            // or you could create a new account
        }
    });
}));