Current behavior
The documentation states here that:
...we passed the RolesGuard type (instead of an instance), leaving responsibility for instantiation to the framework and enabling dependency injection.
So I'm expecting to "override" a guard with another, via the module's providers
.
This doesn't work as expected.
Interestingly enough, injecting the same service via the controller's constructor does yield the correct service, though.
Input Code
Simple: https://github.com/dima-gusyatiner/nestjs-guards-override/tree/only-app-module
With Another module, using exports
: https://github.com/dima-gusyatiner/nestjs-guards-override/tree/master
Controller:
@Controller()
@UseGuards(AuthGuard)
Module:
@Module({
controllers: [AppController],
providers: [
{
provide: AuthGuard,
useClass: AuthOverrideGuard,
}
],
})
Expected behavior
I would expect AuthGuard
to never even be constructed.
Instead, both classes are constructed, and AuthGuard
is used as the guard.
Environment
- Nest version: 8.0.6
- Node version: 16.8.0
- Platform: Linux
Bug Report
I've also opened a ticket for this: https://github.com/nestjs/nest/issues/8011
Question
Am I doing something wrong here, or is this a bug? Anybody can suggest a workaround?