#Injected Services are undefined.

2 messages · Page 1 of 1 (latest)

amber ginkgo
#

Hey guys,

I created a passport jwt auth guard and registered it globally. However, when I try to inject a service to the JwtAuthGuard it fails because the reflector injected in the constructor becomes undefined and it shows the error below.

If I use the decorator UseGuards to call the JwtAuthGuard it works, but in that case I had to explicitly import the services into any module I use the guard in, which is less than ideal for me.

I wonder if anyone has any idea what I might be doing wrong or if this is an expected behaviour. I have also attached a snippet of the guard I want to use.

TypeError: Cannot read properties of undefined (reading 'getAllAndOverride')
    at JwtAuthGuard.canActivate (/app/src/auth/auth.guard.ts:27:37)
export class JwtAuthGuard extends AuthGuard('jwt') {
  constructor(
    private reflector: Reflector,
    private abilityFactory: AbilityFactory,
  ) {
    super();
  }

  canActivate(context: ExecutionContext) {
  // The error is from this line below this.reflector is undefined
    const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
      context.getHandler(),
      context.getClass(),
    ]);

    if (isPublic) {
      return true;
    }

    return !!super.canActivate(context);
  }
}
amber ginkgo
#

Injected Services are undefined.