首页 > 解决方案 > 在 Angular + AngularFire 应用程序中登录后权限被拒绝

问题描述

当我成功登录用户后尝试访问数据库时,我收到错误

permission_denied at /profiles/N97wiXBCMxhB06qQXJ2Bvhcjnts1:客户端无权访问所需数据。

因为,我刚刚对用户进行了身份验证,所以我不明白用户如何没有权限。另一个奇怪的行为是它在我第二次按下调用函数signIn(singInForm)的“登录”按钮时起作用。

登录页面.ts

 signIn(signInForm) {
    this.authService.signInWithEmailAndPassword(signInForm.email, signInForm.password)
    .then(res => {
      const userID = res.user.uid;
      this.fbBackendService.fetchUserProfile(userID).subscribe(userProfile => {
        // DO SOMETHING using the userID
      }, error => {
        // DO SOMETHING
      });
    }).catch(error => {
      // DO SOMETHING
    });
  }

fb-backend.service.ts

constructor(private db: AngularFireDatabase) { }

 
 fetchUserProfile(userID): Observable<IUserProfile> {
    return this.db.object(`/profiles/${userID}`)
      .valueChanges()
      .pipe((take(1), map((response: any) => response as IUserProfile)));
  }

以下是相关的规则集:

{
  "rules": {
    ".write": false,
    ".read": false,
    "profiles": {
      "$uid": {
       ".read": "auth.uid === $uid",
       ".write": "auth.uid === $uid",
       ".indexOn": "company_id"
      }
    }
  }
}

标签: javascriptionic-frameworkgoogle-cloud-firestorefirebase-securityangularfire2

解决方案


您可以尝试降级到 firebase 8.3.0。新版本似乎有问题。


推荐阅读