【问题标题】:auth0 get profile information backsideauth0 获取后台配置文件信息
【发布时间】:2019-11-08 04:54:01
【问题描述】:

我正在尝试从 Auth0 获取用户个人资料信息。它使用管道异步在前端工作

<pre *ngIf="auth.userProfile$ | async as profile">
<code>{{ profile | json }}</code>
</pre>

但是在后端如果我想读取昵称并用它做点什么。我迷路了。

constructor(public auth: AuthService)
ngOnInit() {
   if (this.auth.isAuthenticated$) {
    const result = this.auth.userProfile$;
   }
}

我知道我的变量“结果”是一个 Observable。 但我对 Observable 的这些东西很陌生。我试图获得价值。 如果我使用调试控制台,我可以看到这一行的值:

this.auth.userProfile$.source.value.nickname

但是如果我在我的代码中编写它,我有这个错误:Typescript error: Property 'value' does not exist on type 'Observable'

ngOnInit() {
   if (this.auth.isAuthenticated$) {
    const result = this.auth.userProfile$;
    console.log(this.auth.userProfile$.source.value.nickname); // error here

   }
}

所以有人可以帮助我吗? 谢谢

【问题讨论】:

    标签: auth0


    【解决方案1】:

    您可以通过订阅 observable 访问数据(这就是 async 管道在后台所做的事情)。

    这是一个如何订阅 observable 的示例:

    // import { tap } from 'rxjs/operators';
    // tap is one of many rxjs operators
    // operators allow you to perform operations on data within observables
    
    this.auth.userProfile$.pipe(
      tap(profile => {
        console.log(profile);
      })
    )
    .subscribe(); // this is what allows you access the data within the observable
    

    可观察:https://angular.io/guide/observables

    RXJS 操作员:https://rxjs-dev.firebaseapp.com/guide/operators

    【讨论】:

    • 老实说,这并不能回答我的问题。您编写 Auth0 上可用的代码。我也有这个代码。所以我把它放在我的代码中。我在 console.log 中得到的是一个 Observable。就像我在我的问题中所说的那样。我知道这是一个 Observable。我想从个人资料中读取昵称...或电子邮件或其他信息并进行操作。
    • 哦,好的,您想实际更改 Auth0 中的这些值吗?我误解了这个问题。 (我认为这只是在可观察范围内访问/查看)
    【解决方案2】:

    我终于找到了解决办法:

    在课堂上我输入以下代码:

    export class getInfo implements OnInit {
       private login_info: any;
    
       ngOnInit() {
          this.auth.getUser$().subscribe(val => {
            this.login_info = val;
            console.log('print info', val.nickname);
         });
      }
    }
    
    public useInfo(status: string) {
      console.log('print info', this.login_info.nickname);
    }
    

    所以在 useInfo 方法中,我可以使用从用户配置文件中获取的信息。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 2018-08-07
      • 2013-05-29
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多