Firebase,使用 Angular 和 Nativescript - 从数据库中获取用户分配的值

Firebase, with Angular & Nativescript - Getting user assigned value from Database

我正在尝试在应用程序中设置管理系统以限制对应用程序某些区域的访问。它非常基本,只需使用以下信息在 firebase 中创建一个新对象。 Admin 默认设置为 false,我会手动将其更新为 true,因为要处理的东西很少。

这是我的登录功能

login() {
  this.firebaseService.login(this.user)
      .then(() => {
          this.isAuthenticating = false;
          console.log(this.user.email);
          this.userDetails = <any>this.firebaseService.getUserInfo();
          console.log(this.userDetails);
          this.userDetails.subscribe(users => {
            if(users.user.admin) {
                console.log('yes');
                this.routerExtensions.navigate(["/admin"], { clearHistory: true } );
            }
            else {
                console.log('no');
                this.routerExtensions.navigate(["/"], { clearHistory: true } );
            }
          });
      })
      .catch((message:any) => {
          this.isAuthenticating = false;
      });
  }

console.log(this.userDetails) 正在将 [Object object] 打印到控制台。

这是服务电话

 getUserInfo(): Observable<any> {
  return new Observable((observer: any) => {
     let path = "/Users/"+ BackendService.token;

      let onValueEvent = (snapshot: any) => {
          this.ngZone.run(() => {
              let results = this.handleSnapshot(snapshot.value);
              console.log(JSON.stringify(results));
              observer.next(results);
          });
      };
  });
  }

那么处理我正在尝试做的事情的最有效方法是什么?抱歉,如果这是一个糟糕的问题,我通常使用 AngularFire 身份验证,除了常规 firebase 之外几乎没有其他经验。

考虑使用自定义声明来控制访问并为用户分配管理员权限: https://firebase.google.com/docs/auth/admin/custom-claims

只要知道管理员用户的 uid 或电子邮件,就可以使用管理员 SDK 将该用户设置为管理员。这只是一个 API 电话。

admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
});

然后您强制对用户进行令牌刷新,获取令牌并使用(您对该用户设置的额外声明将传播到其令牌)对其进行解析: https://firebase.google.com/docs/auth/admin/custom-claims#access_custom_claims_on_the_client 然后,您可以根据声明值相应地导航您的用户 (admin: true)。