收到错误FirebaseError:缺少权限或权限不足

时间:2020-08-19 06:29:31

标签: firebase google-cloud-firestore firebase-authentication firebase-security angularfire2

我使用Angular 8开发Web应用程序,并使用@ angular / fire v5.4.2和firebase js SDK v7.8.0连接到firebase。每当我想在Firestore中获取文档时,它总是显示错误

FirebaseError: Missing or insufficient permissions.
    at new FirestoreError (https://localhost:4200/vendor.js:130146:28)
    at JsonProtoSerializer.push.../../node_modules/@firebase/firestore/dist/index.cjs.js.JsonProtoSerializer.fromRpcStatus (https://localhost:4200/vendor.js:145520:16)
    at JsonProtoSerializer.push.../../node_modules/@firebase/firestore/dist/index.cjs.js.JsonProtoSerializer.fromWatchChange (https://localhost:4200/vendor.js:146033:44)
    at PersistentListenStream.push.../../node_modules/@firebase/firestore/dist/index.cjs.js.PersistentListenStream.onMessage (https://localhost:4200/vendor.js:142655:43)
    at https://localhost:4200/vendor.js:142584:30
    at https://localhost:4200/vendor.js:142624:28
    at https://localhost:4200/vendor.js:131493:20
    at ZoneDelegate.invoke (https://localhost:4200/polyfills.js:3690:26)
    at Object.onInvoke (https://localhost:4200/vendor.js:83071:33)
    at ZoneDelegate.invoke (https://localhost:4200/polyfills.js:3689:52)

这是我尝试获取文档时的代码

loginFirebase(): Promise<any> {
  return new Promise((resolve, reject) => {
    firebase.auth().signInAnonymously().then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}

login(username: string, password: string): Promise<IUsers> {
  return new Promise((resolve, reject) => {
     this.loginFirebase().then(userLogin => {
        this.setUser(res).then(() => {
           resolve();
        }).catch(err => {
           reject(err);
        });
     }).catch(errorFirebase => {
        reject(errorFirebase);
     });
  }
}

setUser(data: UserLogin): Promise<any> {
    return new Promise((resolve, reject) => {
      const userData: IUsers = {
        userId: data.doctor.id.toString(),
        userFullName: `${data.doctor.firstName} ${data.doctor.lastName}`,
        userPhoto: data.doctor.profileImage || '',
        userStatus: UserStatus.ACTIVE,
        userType: data.roles[0].role,
        token: data.token,
        sex: data.doctor.sex,
        email: data.name
      };
      try {
        this.afStore.collection(`${environment.firestoreCollection}users`, ref => ref.where('userId', '==', userData.userId.toString()))
          .get()
          .subscribe(doc => {
            resolve(doc)
          }, err => {
             console.error(err);
          });
       } catch (error) {
          this.translate.get('error_message', {error: 'catch saveUser'}).subscribe(err => {
             if (confirm(err)) {
                this.setUser(data);
             } else {
                reject(error);
             }
          });
       }

这是Firestore的规则

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if isAuth(request);
    }
    
    function isAuth(req) {
      return req.auth != null;
    }
  }
}

当Web应用程序匿名登录时,我检查uid是否已在Firebase身份验证中。我在这里可能出错了吗?有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您应该使用auth state observer来确定用户何时实际登录并能够进行经过身份验证的查询。事实证明,如果用户已完全登录,则signInAnonymously返回的承诺实际上并不是指示器。