Let's say I have my module defined as below:
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
// Use ConfigService here
secretOrPrivateKey: 'secretKey',
signOptions: {
expiresIn: 3600,
},
}),
PrismaModule,
],
providers: [AuthResolver, AuthService, JwtStrategy],
})
export class AuthModule {}
Now how can I get the secretKey from the ConfigService in here?
You have to use registerAsync, so you can inject your ConfigService. With it, you can import modules, inject providers and then use those providers in a factory function that returns the configuration object:
JwtModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
secretOrPrivateKey: configService.getString('SECRET_KEY'),
signOptions: {
expiresIn: 3600,
},
}),
inject: [ConfigService],
}),
For more information, see the async options docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With