@Module({
imports: [],
providers: [SupertokensService, AuthService],
exports: [],
controllers: [AuthController],
})
export class AuthModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes('*');
}
static forRoot({
connectionURI,
apiKey,
appInfo,
}: AuthModuleConfig): DynamicModule {
return {
providers: [
{
useValue: {
appInfo,
connectionURI,
apiKey,
},
provide: ConfigInjectionToken,
},
],
exports: [],
imports: [],
module: AuthModule,
};
}
}
The problem with this implementaion I can't use env variables, so I need useFactory to pass ConfigService. Can somebody do that, and give some explanation.
I figure out how to make this work, unfortunately it only works with nest.js version 9 (current latest version). First you need to create a new file. For example
auth.module-definition.ts
. Now in this file we need to createConfigurableModuleBuilder
.we need to set
setClassMethodName('forRoot')
, when we setforRoot
it will create two methods,forRoot
andforRootAsync
. The next step is to extend our createdConfigurableModuleClass
. it should look something like thisforRootAsync
and we get reigister it inapp.module.ts
here I am using
Nest.js Config
, but you don't need to, so use it how you want. I know that my english is not the best, so if you still do not understand you can check these sources https://docs.nestjs.com/fundamentals/dynamic-modules#configurable-module-builder https://trilon.io/blog/nestjs-9-is-now-available#Configurable-module-builder