mikenicholson/passport-jwt

My passport node.js Code is not working

asad149 opened this issue · 4 comments

My Nodejs Passport jwt authentication code is not working
giving this error: ypeError: JwtStrategy requires a secret or key
at new JwtStrategy (C:\MERN STACK\Asad\node_modules\←[4mpassport-jwt←[24m\lib\strategy.js:45:15)
at module.exports (C:\MERN STACK\Asad\config\passport.js:14:5)

Code :

const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const mongoose = require("mongoose");

const User = mongoose.model("users");
const keys = require("../config/keys");

const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrkey = keys.secretOrkey;

module.exports = passport => {
passport.use(
new JwtStrategy(opts, (jwt_payload, done) => {
console.log(jwt_payload);
})
);
};

can you please post content of your config/keys?

my config/keys folder

only have to parameters like:

module.exports={

mongoURI:"....", //My Connection String of mongoDB
secretOrkey:"secret", //Secret Key
}

Thanks. Now I have noticed that in your previous code

const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrkey = keys.secretOrkey;

you use incorrect field. (you have lowercase k in key, but passport actually needs uppercase -> opts.secretOrKey

If you have a reproducible issue with this module, please re-open with a code sample or detailed instructions to reproduce.

Unfortunately I can't provide debugging assistance or support, consider resources like stack overflow or the many passport tutorials available.