【问题标题】:Firebase firestore security rules not workingFirebase Firestore 安全规则不起作用
【发布时间】:2020-08-24 23:19:27
【问题描述】:

我有一个非常简单的 Firestore 数据库,如上图所示。

我想写一个安全规则,这样只有经过身份验证的用户才能进入,但无论我写什么,我总是被拒绝。

我试过了:

rules_version = '2';
service cloud.firestore {
  match /users/{user} {
   allow read, write: if request.auth.uid == user
   match / {docs = **} {
      allow read, write: if request.auth.uid == user
   }
 }
}

我也试过了:

rules_version = '2';
service cloud.firestore {
 match /users/{user} {
   allow read, write: if request.auth.uid == user
   match / {docs = **} {
      allow read, write: if request.auth.uid == user
   }
 }
}

我也试过了:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/{documents=**} {
      allow read, write: if isOwner(userId);
    }
  }

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

这不起作用:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /sessions/{sessionID} {
      allow read, write: if request.auth != null;
    }
  }
}

这也不行:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /sessions/{sessionsID} {
      allow read, write: if request.auth != null;
    }
  }
}

【问题讨论】:

  • 我在整个帖子中看不到 sessions 这个词。或者,也许您发布了任何其他屏幕截图?
  • 我根本没有在规则中使用会话一词 - 但我认为我选择了所有文档,但是它们嵌套在文件夹中?
  • 不确定,我在 Firestore 中的所有规则都以数据库名称开头
  • 你能给我一个适合你的例子吗?我所需要的只是允许经过身份验证的用户读写
  • 这些用户 UID 是否在边会文档中?

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


【解决方案1】:

我试图将我的规则与您的数据库结构进行比较。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /sessions/{sessionID=**} {
      allow read, write: if request.auth.uid != null;
    }
}

现在这应该只允许注册用户获得访问权限。

【讨论】:

  • 我根本看不出这条规则如何匹配任何文档。没有通配符。
  • @DougStevenson 执行了回滚。聊天过程中出现了些许混乱。
猜你喜欢
  • 2023-02-09
  • 2019-01-31
  • 2018-03-19
  • 2021-05-12
  • 2021-11-22
  • 2019-09-06
  • 2018-05-28
  • 1970-01-01
相关资源
最近更新 更多