I'm utilizing APP_INITIALIZER
to load environment-specific variables. The challenge I am facing is needing to access these variables inside my authConfigFactory
, but the factory initiates before the completion of APP_INITIALIZER
within the app configuration.
The library being used is: https://github.com/manfredsteyer/angular-oauth2-oidc
My goal is to utilize the value of
APP_CONFIG.authConfig.allowedUrls
in my auth config factory. How can I ensure that the configuration sets first before the auth factory?
An error encountered in the factory is:
Cannot read property 'authConfig' of undefined
app.module.ts
:
providers: [
AppConfigService,
{
provide: APP_INITIALIZER,
useFactory: (config: AppConfigService) => () => config.load(),
multi: true,
deps: [AppConfigService]
},
{
provide: OAuthModuleConfig,
useFactory: authConfigFactory
}
]
app.config.ts
:
export let APP_CONFIG: AppConfig;
@Injectable()
export class AppConfigService {
constructor(
private injector: Injector
) {}
config = null;
public load() {
const http = this.injector.get(HttpClient);
return http
.get('../assets/config/config.json')
.pipe(
tap(returnedConfig => {
const t = new AppConfig();
APP_CONFIG = Object.assign(t, returnedConfig);
})
)
.toPromise();
}
}
auth-config-factor
:
export function authConfigFactory(): OAuthModuleConfig {
return {
resourceServer: {
allowedUrls: APP_CONFIG.authConfig.allowedUrls,
sendAccessToken: true
}
};
}