Vova Bilyachat
Vova Bilyachat

Reputation: 19474

How to export APP_GUARD from nestjs module?

I have module which implement JWT passport

const appGuard = {
  provide: APP_GUARD,
  useClass: JwtAuthGuard,
};

@Module({})
export class JwtAuthModule {
  static forRoot(options: { jwks: string, excludePath: string[] }): DynamicModule {
    const exportableProviders = [
      { provide: JWKS_URI, useValue: options.jwks },
      { provide: EXCLUDE_FROM_AUTH, useValue: options.excludePath },
      appGuard,
    ];
    return {
      module: JwtAuthModule,
      providers: [
        ...exportableProviders,
        JwtStrategy,
        JwtAuthGuard
      ],
      exports: exportableProviders,
    };
  }
}

But I am getting an error:

Error: Nest cannot export a provider/module that is not a part of the currently processed module (JwtAuthModule). Please verify whether the exported APP_GUARD is available in this particular context.

Possible Solutions:

  • Is APP_GUARD part of the relevant providers/imports within JwtAuthModule?

What is interesting if I debug i see that APP_GUARD has name in providers like: APP_GUARD (UUID: 63cf5b8e-815e-4034-a4a8-c6196d98b612)

Simple work around is to register APP_GUARD in providers at each root module and import module, but i would love to be able just to import this module once if possible...

Upvotes: 0

Views: 759

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70061

APP_GUARD, along with the other APP_* providers, are special provider tokens that Nest allows to be bound multiple times. As soon as it is used it is bound as a global enhancer of whatever type relating back to the token (pipe, guard, interceptor, filter). As it's bound, there's no need to export the enhancer as well, there's nothing else that can make use of it directly, it's bound globally so it's already fine as is. Just remove the APP_GUARD provider you have from the exports and you should be good to go

Upvotes: 2

Related Questions