Skip to content

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

@JayeshC-2598

Description

@JayeshC-2598

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

};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions