javascript - 基于属性值的 Firebase Firestore 安全规则

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

我正在尝试在 Firestore 数据库上编写安全规则,以允许读取 status 值为 published 的集合中的文档。但是,一旦规则发布,就不允许读取该集合的任何文档。

我遵循官方文档中所述的权限设置。 https://firebase.google.com/docs/firestore/security/rules-conditions

但是,它没有按预期工作。

service cloud.firestore {
  match /databases/{database}/documents {

    match /articles/{articleId} {
        allow read: if resource.data.status == 'published';
    }
  }
}
<小时/>

我使用以下 React + Redux 代码来查询文档:

export const getArticles = (limit = 25) => {
  return (dispatch) => {
    dispatch({ type: ARTICLES });

    const db = firebase.firestore();
    const query = db.collection('articles')
                    .orderBy('createdAt', 'desc')
                    .limit(limit);

    query.get()
    .then((snapshot) => {
      const dataArray = [];
      snapshot.forEach(doc => {
        dataArray.push(mergeDocIdAndData(doc.id, doc.data()));
      });
      getArticlesSuccess(dispatch, dataArray)
        .then(() => dispatch(push('/articles')));
    })
    .catch(() => getArticlesFail(dispatch));
  };
}

最佳答案

您错过了文档中的一个特定点:您的查询失败“因为它不包含与您的安全规则相同的约束”和“安全规则不是过滤器”。请参阅https://firebase.google.com/docs/firestore/security/rules-query#queries_and_security_rules

换句话说,您的查询应该是这样的:

const query = db.collection('articles').where("status", "==", "published")...

关于javascript - 基于属性值的 Firebase Firestore 安全规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50526787/

相关文章:

javascript - 如何有选择地将鼠标事件传递给底层 svg 元素

javascript - React Native NetInfo 'hander is not a function' 错误代码

javascript - Firebase/JS - 在加载页面之前触发 JavaScript?

ios - 尝试显示 FirebaseStorage 中我的用户的用户名

ssl - 使用 cloud_proxy_sql 设置自定义 CA 证书

tensorflow - CuDNNLSTM : Failed to call ThenRnnForward

python - 在 cloudshell (Google App Engine) 中的何处查找已部署的应用程序文件?

javascript - 如何在选择后插入节点?

javascript - 获取动态 anchor 标签的值并传递给下一页

android - 知道 Firebase 何时完成 API 调用?