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 "^"
– Renato Junior
That there is generated in the same terminal
– Hiago Machado