我正在使用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> 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> at C:\Users\\OneDrive - GfK\Documents\IPCA\Programação Web\PWeb - Projecto\FTA\backend\node_modules\express-jwt\dist\index.js:157:54<br> 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> 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> 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> at processTicksAndRejections (node:internal/process/task_queues:96:5)发布于 2022-06-02 17:46:13
根据更新: isRevoked函数有(req、有效负载、cb),现在它可以返回承诺并接收(req,token)。令牌有头和有效负载。
IsRevoked = (req: express.Request,令牌: jwt.Jwt =未定义) => Promise
这对我起了作用:
async function isRevoked(req, token){
if(!token.payload.isAdmin) {
return true;
}
}发布于 2022-05-11 17:31:03
const { expressJwt} = require('express-jwt')
exports.authJwt = expressJwt ({ secretJwt,算法:‘HS256 256’,//https://jwt.io/ isRevoked: isRevoked }).
https://stackoverflow.com/questions/72117000
复制相似问题