为什么 Cloud Firestore 不允许通过 Firebase Auth 验证进行访问?

Why is Cloud Firestore not allowing access with Firebase Auth validation?

我有一个具有以下安全规则的 Cloud Firestore 数据库:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
    match /data/{userId=**} {
        allow read: if request.auth.uid == userId;
    }
  }
}

我有一个 React 应用程序,它正在使用实时侦听器读取单个文档。我收到消息 “快照侦听器中未捕获的错误:FirebaseError:权限缺失或不足。”

如果我将 user.uid 打印到控制台,它是正确的。我已验证读取是在用户通过身份验证后执行的。 user.uid 匹配上面的“userId”文档。

如果我将 Firestore 安全规则更改为以下内容,读取工作没有问题:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
    match /data/{userId=**} {
        allow read: if request.auth != null;
    }
  }
}

如果有任何不同,用户使用 signInWithCustomToken 方法登录。随着规则的更改,似乎以某种方式没有将正确的 uid 传递给 Firestore 请求,但我不明白为什么。有人遇到过这样的问题吗?

根据这个 part of the documentation

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}

应该是{userID}而不是{userId=**}才能访问变量