У меня уже была эта проблема, и я попробовал много возможностей без удачи, единственное решение - я использовал ngrx / store
В app.config.ts вы можете отправить действие, чтобы сохранить конфигурацию вмагазин, и вы можете получить его после этого в других сервисах, выполнив: store.select () Подпишитесь на него и выполняйте свой контроль
app.module.ts
providers: [
AuthService, // oidc-client.ts where i need the config from json
DataService,
ConfigService,
{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [ConfigService],
multi: true,
},
config.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { IAppStore } from '../models/store.model';
import * as ConfigActions from '../store/actions/config.actions';
@Injectable()
export class ConfigService {
public config: any = {};
constructor(private http: HttpClient, private store: Store<IAppStore>) {}
getConfig(key: string): string {
return this.config[key] || '';
}
public loadConfig() {
return new Promise((resolve, reject) => {
this.http.get('app-config.json').subscribe(
(response) => {
this.config = response;
this.store.dispatch(new ConfigActions.AddConfig(response)); // dispatch action to update the store
resolve(true);
}
);
});
}}
AuthService
import { Log, User, UserManager, WebStorageStateStore } from 'oidc-client';
...
@Injectable()
export class AuthService {
private _userManager: UserManager;
public _user: User;
constructor(
private store: Store<IAppStore>
private httpClient: HttpClient,
private route: Router,
private configs: ConfigService
) {
this.store.select('appConfig').subscribe((configdata) => {
Log.logger = console;
const config = {
authority: configdata.stsAuthority,
client_id: configdata.clientId,
redirect_uri: `${configdata.clientRoot}/#/auth-callback#`,
scope: 'openid profile fusionApi.full_access',
response_type: 'id_token token',
post_logout_redirect_uri: `${configdata.clientRoot}?postLogout=true`, // delet all stored tokens after logout
userStore: new WebStorageStateStore({ store: window.localStorage }),
automaticSilentRenew: true,
silent_redirect_uri: `${configdata.clientRoot}/assets/html/silent-refresh-redirect.html`,
};
this._userManager = new UserManager(config);
this._userManager.getUser().then((user) => {
if (user && !user.expired) {
this._user = user;
}
});
...
}
login(): Promise<any> {
return this._userManager.signinRedirect();
}
...