Imagine this you are coding happy, configuring your module has auth.module.ts using on your register the classic call to get env values, Classic .env file on you project and get an error like this…

Error: secretOrPrivateKey must have a value
maybe your import for JwModule luke like this.
@Module({
imports: [
UserModule,
PassportModule,
JwtModule.register({
secret: process.env.JWT_SECRET,
signOptions: {
expiresIn: process.env.EXPIRES_IN,
audience: process.env.APP_URL,
},
}),
],
controllers: [AuthController],
providers: [AuthService, LocalStrategy, JwtStrategy],
but not, is not your foul this happen because the time when de module import begun is previus load or access to normal and tipical process.env.VALUES so to solved this error the better way look like this.
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
UserModule,
PassportModule,
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
secret: config.get('JWT_SECRET'),
signOptions: {
expiresIn: config.get('EXPIRES_IN'),
audience: config.get('APP_URL'),
},
}),
})
],
controllers: [AuthController],
providers: [AuthService, LocalStrategy, JwtStrategy],
})
No hay comentarios:
Publicar un comentario