Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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 在加载页面之前,auth0中的配置文件对象为空_Angular_Auth0_Auth0 Delegated Admin - Fatal编程技术网

Angular 在加载页面之前,auth0中的配置文件对象为空

Angular 在加载页面之前,auth0中的配置文件对象为空,angular,auth0,auth0-delegated-admin,Angular,Auth0,Auth0 Delegated Admin,我已经按照auth0的文档实现了配置文件图片和其他配置文件数据。在加载页面之前,auth0中的配置文件对象为空。 这是我从navbar组件调用配置文件数据的代码 ngOnInit() { if (this.auth.userProfile) { this.profile = this.auth.userProfile; return; } if (this.auth.authenticated) { this.auth.ge

我已经按照auth0的文档实现了配置文件图片和其他配置文件数据。在加载页面之前,auth0中的配置文件对象为空。 这是我从navbar组件调用配置文件数据的代码

ngOnInit() {
    if (this.auth.userProfile) {
        this.profile = this.auth.userProfile;
        return;
    }
    if (this.auth.authenticated) {
        this.auth.getProfile((err, profile) => {
            this.profile = profile;
        });
    }
}
下面是来自auth.service的getProfile方法

public getProfile(cb): void {
    const accessToken = localStorage.getItem('access_token');
    if (!accessToken) {
        throw new Error('Access token must exist to fetch profile');
    }    
    const self = this;
    this.auth0.client.userInfo(accessToken, (err, profile) => {
        if (profile) {
            self.userProfile = profile;
        }
        cb(err, profile);
    });
}

登录后,我收到错误“访问令牌必须存在才能获取配置文件”,但如果我重新加载它,我就看不到它。

我遇到了与@Kaws相同的问题

它在教程中起作用,但当我尝试在我的解决方案中实现它时,我希望在存储访问令牌之前加载的导航栏中显示“昵称”

解决这一问题的方法是使用

AuthService.ts:

import { Observable, Observer } from 'rxjs';
// ...
private observer: Observer<string>;
userImageChange$: Observable<string> = new Observable(obs => this.observer = obs);
// ...
public handleAuthentication(): void {
  this.auth0.parseHash((err, authResult) => {
    if (authResult && authResult.accessToken && authResult.idToken) {
      window.location.hash = '';
      this.setSession(authResult);
      this.getProfile();
      this.router.navigate(['/controlpanel']);
    } else if (err) {
      this.router.navigate(['/controlpanel']);
      console.log(err);
    }
  });
}

public getProfile(): void {
  const accessToken = localStorage.getItem('access_token');
  if (!accessToken) {
    throw new Error('Access token must exist to fetch profile');
  }
  const self = this;
  this.auth0.client.userInfo(accessToken, (err, profile) => {
  if (profile) {
      this.observer.next(profile.picture);
    }
  });
}

是否在本地存储中设置accessToken?如何设置?是的,本教程是iam following accessToken设置的,因为如果重新加载页面,则会获取配置文件。您是否在setSession方法中更改了令牌到期时间?我相信他们的例子,它会在一秒钟后过期。如果重新加载页面,则会访问配置文件数据。我猜在登录事务完全完成之前会调用getProfile方法,并且在本地存储中保存了一个访问令牌。您是否能够安排一些事情,以便在尝试获取配置文件之前确保已保存访问令牌?
 userImage: string;

  constructor(private auth: AuthService) {}

  ngOnInit() {
    this.auth.userImageChange$.subscribe(image => this.userImage = image);
  }