Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
NestJS setMetadata not working from authGuard

I have a few decorators that when called, I want to setMetadata to use it in my logging, In my controller, I have these:

  @Post("somePath")
  @Permission("somePermission")
  @UseGuards(JwtAuthGuard)
  @HttpCode(200)
  @Grafana(
    "endpoint",
    "functionalmap"
  )
  async getSubscriptionFromPwebFormFilter(
    @Body(ValidationPipe) someDto: someDtoType
  ): Promise<ISuccessResponse> {
    // some logic
  }

In my decorators I want to set some data into the metadata to use in my logging inteceptor,

Grafana decorator:

  export const Grafana = (functionalMap: string, endpoint: string) =>
    applyDecorators(
      SetMetadata("endpoint", endpoint),
      SetMetadata("functionalMap", functionalMap)
    );

AuthGuard decorator:

  @Injectable()
  export class JwtAuthGuard extends AuthGuard("jwt") {
    constructor(
      private readonly reflector: Reflector,
      private readonly someService: SomeService
    ) {
      super();
    }

    public async canActivate(context: ExecutionContext): Promise<boolean> {
      const role = this.reflector.get<string>("permission", context.getHandler());
      const request = context.switchToHttp().getRequest();
      const { valueToLog } = request.body;
      const jwtToken = request.headers.authorization;

      console.log("check value exist", valueToLog);
      SetMetadata("valueToLog", valueToLog);
  }

Now, in my logging interceptor, I am getting all the values of the metadata this way:

    @Injectable()
    export default class LoggingInterceptor {
      constructor(private readonly reflector: Reflector) {}

      intercept(context: ExecutionContext, next: CallHandler) {
        const executionStart = Date.now();

        return next.handle().pipe(
          tap((responseData) => {
            const response = context.switchToHttp().getResponse<ServerResponse>();
            const { statusCode } = response;
            const valueToLog = this.reflector.get(
              "valueToLog",
              context.getHandler()
            ); // this is undefined
            const endpoint = this.reflector.get("endpoint", context.getHandler()); // this have value
            const functionalMap = this.reflector.get(
              "functionalMap",
              context.getHandler()
            ); // this have value
            
            ... 
            // some logic
          })
        );
      }
    }

In my case, the value of endpoint and functionalMap can be retrieved from the reflector, however, valueToLog is appearing as undefined,

Is setting of metadata not working for auth guard decorator?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda