简体   繁体   English

Nestjs:未将反射器注入自定义装饰器

[英]Nestjs: Reflector not injected to custom decorator

I know there are other questions with the same name on the site but my context is pretty different so I'm going to ask.我知道网站上还有其他同名问题,但我的背景非常不同,所以我要问。

I currently have a Turborepo like so:我目前有一个像这样的 Turborepo:

apps/api
packages/pkg/PkgModule
packages/pkg/auth/AuthModule
packages/pkg/auth/PkgAuthStrategy
packages/pkg/auth/PkgAuthGuard

PkgModule imports and reexports PkgAuthModule and PkgAuthModule looks like so: PkgModule 导入和重新导出 PkgAuthModule 和 PkgAuthModule 看起来像这样:

@Module({ 
 imports: [ConfigModule, PkgAuthModule],
 providers: [PkgService],  
 exports: [PkgService, PkgAuthModule]
})
export class PkgModule {}

and PkgAuthModule looks like so PkgAuthModule 看起来像这样

@Module({
  imports: [PassportModule.register({ session: false }), ConfigModule],
  providers: [PkgAuthStrategy, ConfigService],
  exports: [PkgAuthStrategy]
})
export class PkgAuthModule {}

My strategy just returns true for now.我的策略暂时返回 true。 My guard extends AuthGuard('pkg-auth') .我的守卫扩展AuthGuard('pkg-auth') My problem is in my auth guard, when I use this.reflector.getAllAndOverride<boolean> , reflector evaluates to undefined.我的问题出在我的 auth guard 中,当我使用this.reflector.getAllAndOverride<boolean>时,反射器的计算结果为未定义。

AuthGuard:授权卫士:

@Injectable()
export class PkgAuthGuard extends AuthGuard('pkg-auth') {
  constructor(private reflector: Reflector) {
    super()
  }

  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    console.log(this.reflector)
    const isPublic = this.reflector.getAllAndOverride<boolean>('public', [
       context.getHandler(),
       context.getClass(),
    ])

    if (isPublic) return true

    return super.canActivate(context)
  }
}

In api, I simply import PkgModule.在 api 中,我简单地导入了 PkgModule。 The guard when used in an api controller does not inject Reflector properly.在 api controller 中使用时,守卫没有正确注入 Reflector。

Guard definition and usage are like so:守卫的定义和用法是这样的:

export const Protected = () => UseGuards(PkgAuthGuard)

api.controller: api.controller:

@Protected()
@Post('/<endpoint>')
public async endpoint(@Body() body: any) {
...
}

Little background: I have 2 auth strategies, Firebase and Pkg.小背景:我有 2 个身份验证策略,Firebase 和 Pkg。 PkgAuth is used to validate Webhook pushers. PkgAuth 用于验证 Webhook 推送器。 Firebase is used by the user and is working fine. Firebase 已被用户使用,运行良好。 They resemble each other in setup, only notable difference is PkgAuth is in a separate package in my monorepo.它们在设置上彼此相似,唯一显着的区别是 PkgAuth 在我的 monorepo 中位于单独的 package 中。

Found the solution accidentally!无意中找到了解决方案! I realized ConfigModule wasn't injected either so began investigating more vigorously.我意识到ConfigModule也没有被注入,所以开始更积极地调查。 Turns out, because my module is in a separate Turborepo package that is then bundled and used by my Turborepo App, I needed to define all the Nestjs dependencies again:结果是,因为我的模块位于一个单独的 Turborepo package 中,然后被我的 Turborepo 应用程序捆绑和使用,我需要再次定义所有 Nestjs 依赖项:

$ yarn add @nestjs/common @nestjs/core reflect-metadata rxjs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM