express-jwt - 类型错误:完成不是一个函数

问题描述 投票:0回答:3

我正在使用express-jwt来控制启动项目的用户权限,但是当我尝试请求访问(使用Postman)只有管理员可以访问的api时,出现错误。 这是我的 authJwt 文档:

import { expressjwt } from "express-jwt";
import dotenv from 'dotenv';


dotenv.config();
const secretJwt = process.env.JWT_SECRET;

function authJwt() {
    return expressjwt({
        secret: secretJwt, 
        algorithms: ['HS256'], //https://jwt.io/
        isRevoked: isRevoked 
    })
    .unless({ 
        path: 
        [
            '/users/login',
            '/users/register',        
            { 
                url: /\/categories(.*)/, // (https://regex101.com/)
                methods: ['GET', 'OPTIONS'] 
            }
        ]
    })
}

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

export default authJwt;

我收到的错误是:

TypeError: done is not a function<br> &nbsp; &nbsp;at Object.isRevoked (file:///C:/Users/OneDrive%20-%20GfK/Documents/IPCA/Programa%C3%A7%C3%A3o%20Web/PWeb%20-%20Projecto/FTA%20/backend/middlewares/jwt.js:42:9)<br> &nbsp; &nbsp;at C:\Users\\OneDrive - GfK\Documents\IPCA\Programação Web\PWeb - Projecto\FTA\backend\node_modules\express-jwt\dist\index.js:157:54<br> &nbsp; &nbsp;at step (C:\Users\rui.lopes\OneDrive - GfK\Documents\IPCA\Programação Web\PWeb - Projecto\FTA\backend\node_modules\express-jwt\dist\index.js:56:23)<br> &nbsp; &nbsp;at Object.next (C:\Users\rui.lopes\OneDrive - GfK\Documents\IPCA\Programação Web\PWeb - Projecto\FTA\backend\node_modules\express-jwt\dist\index.js:37:53)<br> &nbsp; &nbsp;at fulfilled (C:\Users\rui.lopes\OneDrive - GfK\Documents\IPCA\Programação Web\PWeb - Projecto\FTA\backend\node_modules\express-jwt\dist\index.js:28:58)<br> &nbsp; &nbsp;at processTicksAndRejections (node:internal/process/task_queues:96:5)
javascript node.js express jwt
3个回答
2
投票

根据更新: isRevoked 函数有 (req, Payload, cb),现在它可以返回一个 Promise 并接收 (req, token)。令牌有标头和有效负载。

IsRevoked = (req:express.Request, token:jwt.Jwt | undefined) => Promise

这对我有用:

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

0
投票

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

exports.authJwt = expressJwt ({ 秘密:秘密Jwt, 算法:['HS256'],//https://jwt.io/ 已撤销: 已撤销 })....


0
投票
async function isRevoked(req, token) {
    if (!token.payload.isAdmin) {
        console.log(token.payload.isAdmin)
        return true
    }
    console.log(token.payload.isAdmin)
    
}

这对我有用

© www.soinside.com 2019 - 2024. All rights reserved.