I would like to know why there is a difference between explicitly importing a ConfigModule set with the isGlobal:true option from app.module.ts through import to another module (auth.module.ts) and importing it without specifying it in the import.
Here’s how you configured the ConfigModule in App.module.ts. You set the ConfigModule to load the value of aws-secret-manager as an environment variable with ‘load’, and the option is Global:true.
@Module({
imports: [
ConfigModule.forRoot({
ignoreEnvFile: true,
isGlobal: true,
cache: true,
validationSchema: Joi.object({
NODE_ENV: Joi.string()
.valid('dev', 'ci', 'test', 'qa', 'production')
.required(),
DATABASE_URL: Joi.string().required(),
REDIS_URL: Joi.string().required(),
PORT: Joi.number().default(3000),
}),
validationOptions: {
allowUnknown: false,
abortEarly: true,
},
load: [getSecretValue],
}),
RepoModule,
CacheDBModule,
And I want to use ConfigService in AppleStrategy.
Here’s how Auth.Module looks like. It imports ConfigModule.
@Module({
imports: [
forwardRef(() => UserModule),
JwtModule.register({
global: true,
secret: process.env.JWT_SECRET,
}),
ConfigModule,
RouletteModule,
],
controllers: [AuthController],
providers: [
AuthService,
KakaoStrategy,
FacebookStrategy,
NaverStrategy,
AppleStrategy,
EmailStrategy,
],
exports: [AuthService],
})
export class AuthModule {}
And here’s how AppleStrategy is implemented:
@Injectable()
export class AppleStrategy extends PassportStrategy(Strategy, 'apple') {
private static readonly JWKS = createRemoteJWKSet(
new URL('https://appleid.apple.com/auth/keys'),
);
constructor(configService: ConfigService) {
const clientId = configService.get<string>('APPLE_CLIENT_ID');
const teamId = configService.get<string>('APPLE_TEAM_ID');
const keyId = configService.get<string>('APPLE_KEY_ID');
const redirectUrl = configService.get<string>('APPLE_REDIRECT_URL');
const decodedBuffer = Buffer.from(
configService.get<string>('APPLE_PRIVATE_KEY'),
'base64',
);
const privateKey = decodedBuffer.toString('utf-8');
super(
{
clientID: clientId,
teamID: teamId,
keyID: keyId,
callbackURL: redirectUrl,
privateKeyString: privateKey,
},
AppleStrategy.verify,
);
}
Here, I cannot retrieve values using configService.get(). It outputs undefined.
However, when I remove ConfigModule in Auth.module.ts, I can retrieve values using configService.get().
I want to understand why there is a difference when explicitly importing the ConfigModule set with isGlobal:true option in app.module.ts into another module (auth.module.ts) and when not.
when I remove ConfigModule in auth.module.ts, it is work well.
like this:
@Module({
imports: [
forwardRef(() => UserModule),
JwtModule.register({
global: true,
secret: process.env.JWT_SECRET,
}),
RouletteModule,
],
controllers: [AuthController],
providers: [
AuthService,
KakaoStrategy,
FacebookStrategy,
NaverStrategy,
AppleStrategy,
EmailStrategy,
],
exports: [AuthService],
})
export class AuthModule {}
Enoch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.