ferrerojosh / nest-keycloak-connect

keycloak-nodejs-connect module for Nest
MIT License
318 stars 123 forks source link

Cannot read properties of undefined (reading 'getAllAndOverride') #183

Closed js5253 closed 1 month ago

js5253 commented 6 months ago

We are simply loading the Keycloak config and declaring the guards in the providers:

imports: [
KeycloakConnectModule.registerAsync({
      useExisting: KeycloakConfigService,
      imports: [KeycloakConfigModule],
    })
],
providers: [
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
    {
      provide: APP_GUARD,
      useClass: ResourceGuard,
    },
    {
      provide: APP_GUARD,
      useClass: RoleGuard,
    },
  ],

We have a module and a service that configures the package:


//keycloak-config.module.ts
@Module({
  imports: [LoggingModule],
  providers: [KeycloakConfigService],
  controllers: [],
  exports: [KeycloakConfigService],
})
export class KeycloakConfigModule {}

// keycloak-config.service.ts
@Injectable()
export class KeycloakConfigService implements KeycloakConnectOptionsFactory {
  constructor(private readonly logger: HttpContextualLoggerService) {
    logger.info('Keycloak Config Service Started!')
  }
  createKeycloakConnectOptions(): KeycloakConnectOptions {
    return {
      authServerUrl: 'http://localhost:8080',
      realm: '...',
      clientId: '...',
      secret: null,
      policyEnforcement: PolicyEnforcementMode.PERMISSIVE,
      tokenValidation: TokenValidation.ONLINE,
    }
  }
}

We get the following error on an incoming request:

TypeError: Cannot read properties of undefined (reading 'getAllAndOverride')\n    at AuthGuard.<anonymous> (/Users/jsanchez/project/node_modules/nest-keycloak-connect/dist/guards/auth.guard.js:71:50)
   at Generator.next (<anonymous>)
    at /Users/jsanchez/project/node_modules/nest-keycloak-connect/dist/guards/auth.guard.js:43:71
    at new Promise (<anonymous>)
   at __awaiter (/Users/jsanchez/project/node_modules/nest-keycloak-connect/dist/guards/auth.guard.js:39:12)
    at AuthGuard.canActivate (/Users/jsanchez/project/node_modules/nest-keycloak-connect/dist/guards/auth.guard.js:69:16)
    at GuardsConsumer.tryActivate (/Users/jsanchez/project/node_modules/@nestjs/core/guards/guards-consumer.js:15:34)
    at canActivateFn (/Users/jsanchez/project/node_modules/@nestjs/core/router/router-execution-context.js:134:59)
    at /Users/jsanchez/project/node_modules/@nestjs/core/router/router-execution-context.js:42:37
    at /Users/jsanchez/project/node_modules/@nestjs/core/router/router-proxy.js:9:23"

@nestjs/common@npm:9.3.9 [1b2ba] (via npm:^9.0.0 [1b2ba])

ferrerojosh commented 6 months ago

I'll check on this, maybe reflector is no longer being instantiated in Nest.

akalana commented 1 month ago

@js5253 Hey, I am also suffering from the same issue. I am using latest nestjs version.