Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!