mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.97k stars 214 forks source link

My passport node.js Code is not working #195

Closed asad149 closed 4 years ago

asad149 commented 4 years ago

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); }) ); };

netchkin commented 4 years ago

can you please post content of your config/keys?

asad149 commented 4 years ago

my config/keys folder

only have to parameters like:

module.exports={

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

netchkin commented 4 years ago

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

mikenicholson commented 4 years ago

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.