【问题标题】:Angular Auth0 - get email claim if authenticatedAngular Auth0 - 如果经过身份验证,则获取电子邮件声明
【发布时间】:2021-03-29 04:28:06
【问题描述】:

如果用户已经登录,我想收到电子邮件声明。

this.auth.idTokenClaims$.forEach(e => console.log(">>>>>>>>>>>>>"+e.email));

正在打印电子邮件

我想写这样的东西(因为 e 可以为空)

if((this.auth.isAuthenticated$ | async) === false){
      this.auth.idTokenClaims$.forEach(e => {
      this.cService.dash(e.email).subscribe(
        data => { this.yeah = data},
        err => console.error(err),
        () => console.log('Main Content ')

      )}
      );
   } else {

    this.cService.dash(null).subscribe(
      data => { this.yeah = data},
      err => console.error(err),
      () => console.log('Main Content from else '));
   }

问题: if((this.auth.isAuthenticated$ | async) === false){

它说总是评估为假,因为

此条件将始终返回 'false',因为类型 'number' 和 'boolean' 没有重叠。ts(2367)

算术运算的左边必须是'any'类型, 'number'、'bigint' 或枚举类型.ts(2362)

这几行

谁能帮我解决这个问题

【问题讨论】:

    标签: angular auth0


    【解决方案1】:

    如果 isAuthenticated$Observable 或任何类型的 Subject 我会尝试将 2 个流构造成 pipe 并执行类似的操作

    this.auth.isAuthenticated$.pipe(
      skipWhile( isAuthenticated => isAuthenticated),
      switchMap( () => this.auth.idTokenClaims$ ),
      ...
    ).subscribe()
    
    this.auth.isAuthenticated$.pipe(
      skipWhile( isAuthenticated => !isAuthenticated),
      switchMap( () => this.cService.dash(null) ),
      ...
    ).subscribe()
    

    我的主要观点是,如果你能给出每个有问题的变量的类型,那么最后的 $ 符号主要代表 SubjectObservable 所以我不太明白 forEach 是什么必须不订阅

    【讨论】:

    • 谢谢。我做了一个小改动,效果很好 - 删除了 if - else 部分并使用了 ?this.cService.dash(e?.email).subscribe(
    • @KrisSwat 欢迎您!我的回答解决了你的问题吗?
    • 我使用了不同的方法,如之前的评论中所述。 if 和 else 中不再有重复的代码
    猜你喜欢
    • 2020-01-07
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多