代码之家  ›  专栏  ›  技术社区  ›  Bohao LI

如何使用FireBase规则

  •  1
  • Bohao LI  · 技术社区  · 6 年前

    我正在尝试整合 火碱 对我 角度 应用程序。我从互联网上阅读了很多关于这个主题的教程,大多数教程建议我为我的FireBase数据库定义以下规则(这意味着 允许读写权限):

    {
      "rules": {
        ".read": true,
        ".write": true
      }
    }
    

    但默认情况下,我将其视为数据库规则,如下所示:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write;
        }
      }
    }
    

    当我试图添加大多数教程建议的规则(规则…)时,出现了错误: 'Error saving rules - Line 9: Unexpected '{'. (我还尝试用教程建议的规则替换原始规则,同样的错误)

    似乎所有的教程都没有提到“service cloud.firestore…”(或者它们已经过时了?)

    那么定义规则的正确方法是什么呢? 我要做的是使“规则…”的事情起作用,因为当我试图在Angular应用程序中访问我的FireBase数据库时,我得到了以下错误:

    permission_denied at /employees: Client doesn't have permission to access the desired data
    



    所有的代码都来自这个 post ,可以在上找到源代码 git .

    2 回复  |  直到 6 年前
        1
  •  2
  •   Chris Kon    6 年前

    我也有同样的问题。默认情况下,firebase会显示云FireStore,而不是实时数据库。转到FireBase控制台-->数据库,然后选择实时数据库(是您的教程使用的数据库)。

    你可以在这里找到它:

    如果您想使用云FireStore,您需要的(允许读写权限)是:

    service cloud.firestore{
    匹配/数据库/数据库/文档{
    匹配/文档=**{
    允许读、写:如果为真;
    }
    }
    }
    

    您可以在cloud firestore security section或在realtime database security section找到更多信息。

    在选择实时数据库(您的教程使用的数据库)之前。

    你可以在这里找到它:

    enter image description here

    如果您想使用云FireStore,您需要的(允许读写权限)是:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    有关更多信息,请访问Cloud Firestore security section或ATRealtime Database security section

        2
  •  0
  •   Rodrigo Mata    6 年前

    对于FireBase应用程序,可以使用以下规则:

     {
       "rules": {
         "users": {
           "$uid": {
             ".write": "$uid === auth.uid"
           }
         }
       }
     }
    

    这里有一个链接指向 the documentation