TypeError: ExtractJwt.fromAuthHeader is not a function

1

I'm having some problems with Passaport.js.

When I try to run my api, the terminal returns an error. I went in the file in question and did not find anything wrong.

I searched the net, but the solutions I found left the code to what I have now. So I do not know what could be wrong.

The error returned is this:

/var/www/html/express/app/learnapp/auth.js:9
  jwtFromRequest: ExtractJwt.fromAuthHeader()
                             ^

TypeError: ExtractJwt.fromAuthHeader is not a function
    at Object.<anonymous> (/var/www/html/express/app/learnapp/auth.js:9:30)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/var/www/html/express/app/learnapp/app.js:5:12)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)

The code for the auth.js file is this:

var passport = require("passport");
var passportJWT = require("passport-jwt");
var users = require("./users.js");
var cfg = require("./config.js");
var ExtractJwt = passportJWT.ExtractJwt;
var Strategy = passportJWT.Strategy;
var params = {
  secretOrKey: cfg.jwtSecret,
  jwtFromRequest: ExtractJwt.fromAuthHeader()
};

module.exports = function() {
  var strategy = new Strategy(params, function(payload, done) {
    var user = users[payload.id] || null;
    if (user) {
      return done(null, {id: user.id});
    } else {
      return done(new Error("User not found"), null);
    }
  });
  passport.use(strategy);
  return {
    initialize: function() {
      return passport.initialize();
    },
    authenticate: function() {
      return passport.authenticate("jwt", cfg.jwtSession);
    }
  };
};
    
asked by anonymous 17.08.2017 / 19:34

1 answer

5

Migrating from 2.x.x to 3.x.x

The extractor was replaced with ExtractJwt.fromAuthHeaderAsBearerToken (). The removal of ExtractJwt.fromAuthHeader () was done to clearly change the API so any code relying on the old API would clearly break.

Check Official Link

Migrating from 2.x.x to 3.x.x

The extractor has been changed to ExtractJwt.fromAuthHeaderAsBearerToken() . The removal of ExtractJwt.fromAuthHeader() was clearly made to change the API so that any code depending on the old one will break.

    
18.08.2017 / 15:23