Skip to main content

Using a Guard

The ClsGuard can be also used set up the CLS context. While it is not a "guard" per-se, it's the second best place to set up the CLS context, since after a middleware, it is the first piece of code that the request hits.

To use it, pass its configuration to the guard property to the ClsModule.forRoot() options:

Automatically

Use mount: true

app.module.ts
@Module({
imports: [
ClsModule.forRoot({
guard: { mount: true },
}),
],
// ...
})
export class AppModule {}

Manually

If you need any other guards to use the ClsService, it's preferable to mount ClsGuard manually as the first guard in the root module:

app.module.ts
@Module({
imports: [
ClsModule.forRoot({
guard: { mount: false }
}),
]
providers: [
{
provide: APP_GUARD,
useClass: ClsGuard,
},
],
// ...
})
export class AppModule {}

or mount it directly on the Controller/Resolver with

@UseGuards(ClsGuard);
caution

Please note: since the ClsGuard uses the AsyncLocalStorage#enterWith method, using the ClsGuard comes with some security considerations!