【问题标题】:Angular2+ auth guard with firebase realtime database带有firebase实时数据库的Angular2+ auth guard
【发布时间】:2018-06-11 01:41:03
【问题描述】:

我正在尝试根据用户在 firebase 实时数据库上的数据设置角度路线的保护。我已将管理员权限设置为仪表板:在用户的实时数据库部分 (like this picture) 上为 true。只有当用户有仪表板时,我才允许输入一些特定的路线:他/她的数据库上的真实属性。我试过下面的代码。但它总是重定向到根路由(localhost:4200)

    canActivate(): Observable<boolean> {

    return this.firebaseAuth.authState.map(auth => {
      if (auth) {
        this.authService.getUserData(auth.uid).subscribe(userData => {
          if (userData['dashboard'] === true) {
            return true;
          } else {
            this.router.navigate(['/login']);
            return false;
          }
        })
      } else {
        this.router.navigate(['/login']);
        return false;
      }
    });
}

【问题讨论】:

    标签: angular firebase firebase-realtime-database firebase-authentication


    【解决方案1】:
    canActivate(): Observable<boolean> {
    
        return this.firebaseAuth.authState.switchMap(auth => {
            if (auth)
                return this.authService.getUserData(auth.uid).map(userData => {
                    if (userData['dashboard'] === true)
                        return true;
    
                    this.router.navigate(['/login']);
                    return false;
                });
    
            this.router.navigate(['/login']);
            return of(false);
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      • 2023-04-11
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多