作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 APP_INITIALIZER
加载环境特定变量。我需要在我的 authConfigFactory
中使用这些变量,但工厂会在 APP_INITIALIZER
完成应用程序配置之前继续加载。
我正在使用这个库:https://github.com/manfredsteyer/angular-oauth2-oidc
我想在我的身份验证配置工厂中使用 APP_CONFIG.authConfig.allowedUrls
的值。我怎样才能确保它在 auth 工厂之前首先设置配置。
我在工厂中遇到此错误:无法读取未定义的属性“authConfig”
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
}
};
}
最佳答案
我以前遇到过这个问题,尝试了很多可能性都没有成功,唯一的解决办法是我使用了 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);
}
);
});
}}
授权服务
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();
}
...
关于angular - APP_INITIALIZER 未在出厂前触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52976106/
我是一名优秀的程序员,十分优秀!