【问题标题】:Can't figure out how to write proper security rules in firebase无法弄清楚如何在 Firebase 中编写适当的安全规则
【发布时间】:2020-10-03 12:44:55
【问题描述】:

我开始使用 Firebase 安全规则。为了测试它,我正在编写一个获取集合的函数,然后从集合中获取文档,然后从文档中获取内容。但是,当我尝试从文档中获取内容时它失败了。函数如下

const testAccess = async () => {
  const db = firebase.firestore();
  try {
    const usersCollection = await db.collection('users');
    // console.log('usersCollection: ', usersCollection);
    console.log('gets to here?');
    const docRef = await usersCollection.doc('10158374072639913');
    console.log('gets to here 2?')
    const doc = await docRef.get();
    console.log('gets to here 3!') // not getting to here!
  } catch (e) {
    console.log('error is : ', e);
  }
}

当我运行 docRef.get() 时,我发现错误是:[FirebaseError:缺少或权限不足。]

我不确定为什么这不符合我的安全规则。

我的规则如下

rules_version = '2';
service cloud.firestore {
  match /users/{user} {
    allow read: if true;
    match /{userInfo=**} {
        allow read: if true;
    }
  }
}

这是我的数据库架构

【问题讨论】:

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


    【解决方案1】:

    您缺少数据库规则的根级别:

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /users/{user} {
          allow read: if true;
          match /{userInfo=**} {
              allow read: if true;
          }
        }
      }
    }
    

    没有match /databases/{database}/documents,规则不会匹配数据库中的任何内容,因此每次读取都会被拒绝。

    【讨论】:

    • 谢谢!我也意识到我不需要第三个匹配语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2014-12-18
    • 1970-01-01
    • 2013-03-05
    • 2013-03-05
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多