首页 > 解决方案 > Firebase 安全规则在 get() 或 exists() 操作 PERMISSION_DENIED 时始终为假

问题描述

我完成了我的应用程序,我正在实施安全规则。我目前正在尝试验证银行中是否存在用户,如果存在,他将能够读取和写入特定路径。我正在使用 Firestore:

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

    match /Usuarios/{documentID} {  
        allow read, update, delete: if request.auth.uid != null && request.auth.uid == documentID;
        allow create: if request.auth.uid != null; 
        

     match /Manejo/{manejoID} {
         allow write, read: if exists(/databases/$(database)/documents/$(request.auth.uid)); // The id is never exist... even when it does exist in my database, this operation simply DOES NOT WORK despite the document with the Uid existing in the database
     }
    
    }
    
  
}

编辑1:

match /Manejo/{manejoID} {
         allow write, read: if true; 
    }

这个条件也行不通……

标签: firebasegoogle-cloud-firestorefirebase-security

解决方案


您需要声明要在哪个集合中检查文档是否存在docId == userId

allow write, read: if exists(/databases/$(database)/documents/$(request.auth.uid)); 

你不声明集合。

查看文档中的以下示例,该示例检查集合docId == userId users是否存在该文档。看看/users/后面是如何添加的$(database)/documents

allow create: if request.auth != null && exists(/databases/$(database)/documents/users/$(request.auth.uid))

我知道想要检查Usuarios集合中文档的存在。如果这个假设是正确的,请执行以下操作:

allow write, read: if exists(/databases/$(database)/documents/Usuarios/$(request.auth.uid)); 

请注意,您可能会对这篇 Firebase 博客文章感兴趣。


推荐阅读