“模拟读被拒绝”或“ FirebaseError:缺少权限不足”

时间:2019-06-02 22:20:35

标签: firebase google-cloud-firestore firebase-security-rules

我正在尝试设置Firestore权限规则。这是我的数据,规则和身份验证有效载荷。

enter image description here

enter image description here

enter image description here

service cloud.firestore {
  match /databases/{database}/documents {
    match /questions/{questionId} {
      allow create: if isAdmin() && emailVerified();
      allow read: if isAdmin() && emailVerified();
      allow update: if isAdmin() && emailVerified();
      allow delete: if isAdmin() && emailVerified();
    }

    match /users/{userId} {
        allow get: if isOwner(userId) && emailVerified();
    }
  }

  function isAdmin2() {
    return request.auth.uid == 'CoKtrTQEA8P18v0jtcDUt766rVl1';
  }

  function isAdmin() {
    return getUserData().roles.keys().hasAny(['admin']);
  }

  function getUserData() {
    return get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
  }

  function emailVerified() {
    return request.auth.token.email_verified;
  }

  function isOwner(userId) {
    return request.auth.uid == userId;
  }

  function isSignedIn() {
    return request.auth != null;
  }

}

{
  "uid": "CoKtrTQEA8P18v0jtcDUt766rVl1",
  "token": {
    "sub": "CoKtrTQEA8P18v0jtcDUt766rVl1",
    "aud": "flying-pigs",
    "email": "myemail@example.com",
    "email_verified": true,
    "phone_number": "",
    "name": "",
    "firebase": {
      "sign_in_provider": "google.com"
    }
  }
}

当尝试阅读问题列表时,每次isAdmin()都会返回false,而isAdmin2()则可以正常工作,但是它是硬编码的。

我想念什么?

1 个答案:

答案 0 :(得分:0)

一旦遇到相同的问题,我最终会做类似的事情:

function isAdmin(request) {
    return ['admin'] in getUserData(request).roles
}

确保您的函数将请求作为参数接收,isAdmin和getUserData当前未接收到该请求。这有什么帮助