Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未在typescript decorator中执行express jwt中间件_Typescript_Express_Jwt_Decorator_Express Jwt - Fatal编程技术网

未在typescript decorator中执行express jwt中间件

未在typescript decorator中执行express jwt中间件,typescript,express,jwt,decorator,express-jwt,Typescript,Express,Jwt,Decorator,Express Jwt,我为express编写了我的定制装饰程序。这是控制器装饰器: export function controller(routePrefix: string) { return function (target: Function) { const router = AppRouter.getInstance(); for (let key in target.prototype) { const routeHandler = target.prototype[ke

我为express编写了我的定制装饰程序。这是控制器装饰器:

export function controller(routePrefix: string) {
  return function (target: Function) {
    const router = AppRouter.getInstance();
    for (let key in target.prototype) {
      const routeHandler = target.prototype[key]; //getLogin
      const path = Reflect.getMetadata(
        MetadataKeys.Path,
        target.prototype,
        key
      );
      const method: Methods = Reflect.getMetadata(
        MetadataKeys.Method,
        target.prototype,
        key
      );
      const middlewares =
        Reflect.getMetadata(MetadataKeys.Middleware, target.prototype, key) ||
        [];
      if (path) {
        router[method](
          `${routePrefix}${path}`,
          ...middlewares,
          routeHandler
        );
      }
    }
  };
}
export function use(middleware: RequestHandler | jwtRequestHandler) {
  return function (target: any, key: string, desc: PropertyDescriptor) {
    const middlewares =
      Reflect.getMetadata(MetadataKeys.Middleware, target, key) || [];
    Reflect.defineMetadata(
      MetadataKeys.Middleware,
      [...middlewares, middleware],
      target,
      key
    );
  };
}
以下是
使用
装饰器:

export function controller(routePrefix: string) {
  return function (target: Function) {
    const router = AppRouter.getInstance();
    for (let key in target.prototype) {
      const routeHandler = target.prototype[key]; //getLogin
      const path = Reflect.getMetadata(
        MetadataKeys.Path,
        target.prototype,
        key
      );
      const method: Methods = Reflect.getMetadata(
        MetadataKeys.Method,
        target.prototype,
        key
      );
      const middlewares =
        Reflect.getMetadata(MetadataKeys.Middleware, target.prototype, key) ||
        [];
      if (path) {
        router[method](
          `${routePrefix}${path}`,
          ...middlewares,
          routeHandler
        );
      }
    }
  };
}
export function use(middleware: RequestHandler | jwtRequestHandler) {
  return function (target: any, key: string, desc: PropertyDescriptor) {
    const middlewares =
      Reflect.getMetadata(MetadataKeys.Middleware, target, key) || [];
    Reflect.defineMetadata(
      MetadataKeys.Middleware,
      [...middlewares, middleware],
      target,
      key
    );
  };
}
路线如下:

@控制器(“/api/v1/Portfolions”)

express jwt接受请求并将
user
分配和添加到req obj

`user=req.user`
下面是
checkJwt
中间件:

export const checkJwt = jwt({
  secret: jwksRsa.expressJwtSecret({
    cache: true,
    rateLimit: true,
    jwksRequestsPerMinute: 10,
    // this link is always provided by auth0 providers
    jwksUri: process.env.JWKS_URI as string,
  }),
  //   this is in the API/identifier in auth0.com
  audience: process.env.AUTH0_AUDIENCE,
  //   issuer should end with "/"
  issuer: process.env.ISSUER,
  algorithms: ["RS256"],
});
但是,checkJwt没有得到执行<代码>检查角色
被执行,这意味着我的设置是正确的。由于没有执行checkJwt,
req.user
没有被定义,因此
checkRole
捕获错误。这是
checkRole

export const checkRole = (role: string) => (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  let user: { [key: string]: string } = {};
  user = req.user;
  if (user && user[process.env.AUTH0_NAMESPACE + "/roles"].includes(role)) {
    next();
  } else {
    return res
      .status(401)
      .send("You are not authorized to access this resource!");
  }
};
当我从客户端发布公文包时,checkRole发送此错误“您无权访问此资源!”


我想不出如何使express execute checkJwt生效。

您需要注意装饰师的命令。声明装饰器称为自顶向下,但结果返回自下而上

因此,您的代码应该如下所示:

@post(“/”)
@使用(检查角色(“管理员”))
@使用(检查JWT)
异步createPortfolio(请求,响应){
//你的代码
}

我知道我遗漏了一个简单的要点。非常感谢。