首页 > 解决方案 > Firestore - “缺少权限或权限不足”,但用户已登录且规则正常

问题描述

Firebase 遇到了一个非常“经典”的错误。

登录后,我会立即收到以下消息:“权限缺失或权限不足。”

它从前一段时间就开始工作了,但我无法重建代码的哪种编辑方式,破坏了 Firebase Auth。唯一有意义的调试想法是激活 Firebase 上的日志记录事件,我也这样做了。

在这里我们可以看到“权限”错误: 记录事件 - 错误

在这里我们可以看到错误是通过访问特定路径产生的:Logging events - Path

路径是:users/ngskrFQaVNMngXM0GO4swjv1lpV2

这里变得很奇怪,因为我检查了我的 Firestore 规则,它们很好。在这里你有:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  
    // Scoped Functions

    function isAdmin() {
      return isSignedIn() && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.roles.admin == true;
    }

    // End Scoped Functions

    // Rules

    match /products/{id} {
      allow read: if true;

      match /prices/{id} {
        allow read: if true;
      }

      match /tax_rates/{id} {
        allow read: if true;
      }
    }
    
    match /videos/{id} {
      allow read: if isSignedIn();
      allow write: if isAdmin();
    }
    
    match /workout-weeks/{id} {
      allow read: if isSignedIn();
      allow write: if isAdmin();
    }
    
    match /workout-exercises/{id} {
      allow read: if isSignedIn();
      allow write: if isAdmin();
    }

    match /users/{uid} {
      allow read, write: if
        belongsTo(uid) || isAdmin() //SEEMS TO FAIL HERE IN FE, BUT WORKS IN BE.
      ;
    }
    
    match /announcements/{id} {
      allow read: if isSignedIn();
      allow write: if isAdmin();
    }
    // End Rules
    
  }
}

// Global Functions
function isSignedIn() {
    return request.auth != null;
}
function belongsTo(uid) {
    return isSignedIn() && request.auth.uid == uid;
}
//function hasMonthlySub() {
    //return request.auth.token.subType == "monthly";
//}
//function hasYearlySub() {
    //return request.auth.token.subType == "yearly";
//}
//function isSubscribed() {
    //return hasMonthlySub() || hasYearlySub();
//}
//function existingData() {
    // return resource.data;
//}

为了确保规则正常工作,我在 Firestore 规则中的 Playground 模式中放置了相同的路径。这是工作。如果用户已登录并且正在访问自己的个人资料,则该请求应该没问题。在这种情况下,访问配置文件的用户也是管理员,所以无论如何它都应该工作。

我唯一怀疑的是用户在没有先登录的情况下提出了请求。所以我什至尝试从AngularFireAuth.authState. 没办法...用户已登录:用户已 登录

您对如何进一步调试此代码有任何建议吗?

标签: angularfirebasegoogle-cloud-firestoreangularfire2angularfire

解决方案


推荐阅读