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

284
Views
Acceda a las variables del cuerpo/encabezado de la solicitud dentro de la estrategia jwt - nest js

¿Cómo puedo acceder a las variables de los encabezados/cuerpo en una estrategia?

Actualmente, el JWT se ve así:

 @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor( private readonly appConfigService: AppConfigService, private readonly usersService: UsersService ) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: appConfigService.configs.JWT_SECRET_KEY }); } async validate(payload: { id: string }) { const user = await this.usersService.findBusinessUserById( 'BUSINESS_ID', // TODO: replace this with business id from the request payload.id ); if (user) { return user; } return null; } }

y el guardia se ve así:

 @Injectable() export class JwtAuthGuard extends AuthGuard('jwt') { constructor(private readonly reflector: Reflector) { super(); } canActivate( context: ExecutionContext ): boolean | Promise<boolean> | Observable<boolean> { const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ context.getHandler(), context.getClass() ]); return isPublic ? true : super.canActivate(context); } getRequest(context: ExecutionContext) { const ctx = GqlExecutionContext.create(context); const req = ctx.getContext().req; return req; } }

Si trato de devolver algo que no sea la solicitud de los guardias, obtengo lo siguiente

 TypeError: Cannot read properties of undefined (reading 'authorization') at JwtStrategy._jwtFromRequest

¿Hay alguna forma de obtener el cuerpo de la solicitud o las variables del encabezado en la estrategia?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Respuesta encontrada aquí https://stackoverflow.com/a/68180268/6226852 . Para acceder a la solicitud en el método de validación, necesitaba pasar passReqToCallback y establecer su valor en true al llamar a super dentro del constructor.

about 4 years ago · Santiago Trujillo Report
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!