Typeerror: Extractjwt.fromAuthHeader is not a Function

Asked

Viewed 7,102 times

1

I’m having some problems with Passaport.js.

When I try to run my api, the terminal returns an error. I went to the file in question and found nothing wrong.

I did a web search, but the solutions I found left the code according to what I have now. So I don’t 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 of 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);
    }
  };
};
  • It has a character that apparently wasn’t meant to be there "^"

  • 1

    That there is generated in the same terminal

2 answers

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 was replaced by ExtractJwt.fromAuthHeaderAsBearerToken(). The removal of the ExtractJwt.fromAuthHeader() was clearly made to change the API so that any code depending on the old one breaks.

  • 5

    This is the stack in English, please translate your answer to our language.

  • It worked. Thank you!!

  • I know, this is a pile in Portugal, but I face the same problem some time ago and I found in the pile so I answer. Use google English to Portuguese translator :)

0

For those who are not working with

ExtractJwt.fromAuthHeaderAsBearerToken()

Try it with the following:

ExtractJwt.fromAuthHeaderWithScheme("jwt")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.