mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.96k stars 213 forks source link

TypeError: JwtStrategy requires a secret or key getting this error while running in node.js #252

Open JayeshC-2598 opened 7 months ago

JayeshC-2598 commented 7 months ago

I was trying to run this code but it is giving this error. I tried to resolve it, but nothing worked.

const JwtStrategy = require("passport-jwt").Strategy; const ExtractJwt = require("passport-jwt").ExtractJwt; const Faculty = require("../models/Faculty"); const Student = require("../models/Student"); const Admin = require("../models/Admin");

const keys = require("./key");

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

module.exports = (passport) => { passport.use( new JwtStrategy(opts, async (jwt_payload, done) => { const faculty = await Student.findById(jwt_payload.id); const student = await Faculty.findById(jwt_payload.id); const admin = await Admin.findById(jwt_payload.id);

        if (faculty) {
            return done(null, faculty);
        } else if (student) {
            return done(null, student);
        } else if (admin) {
            return done(null, admin);
        } else {
            console.log("Passport Error");
        }
    })
);

};