Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从angular-oidc客户端中的app.component获取数据时,未定义this.user_Angular_Openid Connect_Oidc Client Js_Oidc Client - Fatal编程技术网

从angular-oidc客户端中的app.component获取数据时,未定义this.user

从angular-oidc客户端中的app.component获取数据时,未定义this.user,angular,openid-connect,oidc-client-js,oidc-client,Angular,Openid Connect,Oidc Client Js,Oidc Client,我正在我的angular项目中使用oidc client.js 现在,用户可以登录和注销,而且工作正常。唯一的问题是,当我想调用app.component中的服务时,在拦截器中,用户没有经过身份验证 身份验证服务是: Injectable({ providedIn: 'root' }) export class AuthService extends BaseService { private _authNavStatusSource = new BehaviorSubjec

我正在我的
angular
项目中使用
oidc client.js

现在,用户可以登录和注销,而且工作正常。唯一的问题是,当我想调用app.component中的服务时,在拦截器中,用户没有经过身份验证

身份验证服务是:

    Injectable({
  providedIn: 'root'
})
export class AuthService extends BaseService {


  private _authNavStatusSource = new BehaviorSubject<boolean>(false);

  authNavStatus$ = this._authNavStatusSource.asObservable();

  private manager = new UserManager(getClientSettings());
  private user: User | null;


  constructor(private http: HttpClient) {
    super();   
    this.manager.getUser().then(user => {
      this.user = user;
      this._authNavStatusSource.next(this.isAuthenticated());
    });
  }

  login() {
    return this.manager.signinRedirect();
  }

  async completeAuthentication() {
    this.user = await this.manager.signinRedirectCallback();
    this._authNavStatusSource.next(this.isAuthenticated());
  }


  isAuthenticated(): boolean {
    return this.user != null && !this.user.expired;
  }

  get authorizationHeaderValue(): string {
    return `${this.user.token_type} ${this.user.access_token}`;
  }

  async signout() {
    await this.manager.signoutRedirect();
  }
}

export function getClientSettings(): UserManagerSettings {
  return {
    authority:environment.authApiURI,
    client_id: 'medilloxdashboard',
    redirect_uri: environment.redirect_uri,
    post_logout_redirect_uri: environment.postLogoutRedirectUri,
    response_type: "id_token token",
    scope: "openid spa profile",
    filterProtocolClaims: true,
    loadUserInfo: true,
    automaticSilentRenew: true,
    silent_redirect_uri:environment.silentRedirectUri,
    userStore: new WebStorageStateStore({ store: localStorage })
  };
}
发生此调用时,我获取
getDashboards失败:this.user未定义
但是我在另一个组件中调用了这个服务,它工作得很好

调用此服务并获得结果的组件位于其 模块


问题出在哪里?

尝试在内部拨打电话,然后
ngAfterViewCheck
而不是
ngOnInit
,因为此时服务尚未启动。小心那会无限期地执行。您可以在用户未定义的情况下继续拨打电话。

要回答您的问题,请选择“在NgafterView中,检查无误,但每次单击每个按钮时都会运行此代码。是否有更好的方法?”

答案是:

  data: any;
  ngAfterViewChecked (): void {
    if(this.data === null || this.data === undefined) {
      this.dashboardService.getDashboards().subscribe(data => this.data = data);
    }
  }

我创建了一个像这样的
初始值设定项

import { AuthService } from "./auth-service";
export function AuthInitializer(auth: AuthService) { return () => auth.isAuthenticated() };
并在
core.module

providers: [

{
  provide: APP_INITIALIZER,
  useFactory: AuthInitializer,
  deps: [AuthService],
  multi: true
}]

这工作得很好。

ngAfterViewChecked
中,这是可以的,但只要我单击每个按钮,这段代码就会运行。有更好的方法吗?下面是我的答案(新的堆栈答案)我发布了一个新的答案,请检查以查看它是否正确
import { AuthService } from "./auth-service";
export function AuthInitializer(auth: AuthService) { return () => auth.isAuthenticated() };
providers: [

{
  provide: APP_INITIALIZER,
  useFactory: AuthInitializer,
  deps: [AuthService],
  multi: true
}]