express-jwt isRevoked given 500 internal server error without any message

Viewed 178

I am new in nodejs,expressjs and trying to authenticate routes using express-jwt package by creating a middleware. Everything is working fine with GET routes but when I am access POST routes and trying to revoke a token, it returns me 500 internal server error without any message. I am very confused that what is the main reason behind this. So guys please help me if possible. My code is as following.

const { expressjwt: expressJwt } = require('express-jwt');

function authJwt() {
    const secret = process.env.secret;
    const api = process.env.API_URL;
    return expressJwt({
        secret,
        algorithms: ['HS256'],
        isRevoked: isRevoked
    }).unless({
        path: [
            {url: /\/api\/v1\/products(.*)/ , methods: ['GET', 'OPTIONS'] },
            {url: /\/api\/v1\/categories(.*)/ , methods: ['GET', 'OPTIONS'] },
            `${api}/users/login`,
            `${api}/users/register`,
        ]
    })
}

async function isRevoked(req,payload,done) {
    if(!payload.isAdmin) {
        return done(null, true);
    }
    done();
}



module.exports = authJwt
2 Answers

this is how I solve this problem:

async function isRevoked(req, token) {
   
    if(!token.payload.isAdmin) {
        return true
    }
     return undefined;
}

isRevoked receives either undefined or true. So if you find the user is not an admin return undefined else return true

async function isRevoked(req, token) {
   
    if(!token.payload.isAdmin) {
        return true
    }
     return undefined;
}

this code, upon running this api in postman,"localhost:3000/api/v1/products", it returns anauthorised error even with bearers token loaded.

Related