【问题标题】:Firebase Rules are not working. Returning data even when I set up rules in FirestoreFirebase 规则不起作用。即使我在 Firestore 中设置规则也返回数据
【发布时间】:2019-06-03 21:27:31
【问题描述】:

我设置了 Firebase 函数来调用我的 Firestore。我正在使用 admin.auth() 并返回数据。我在 Firestore 规则部分设置了自定义规则,但函数没有遵循规则,即当我在 Postman 中使用 URL 时,我不应该获取数据,因为它不满足“如果读取,写入:如果请求” .auth != null”。我该如何解决这个问题?

这是我的 Firebase 功能代码:

const admin = require('firebase-admin');

module.exports = function(req, res) {
  const uid = req.body.uid;

  admin
    .auth()
    .getUser(uid)
    .then(user => {
      admin
        .firestore()
        .collection('discover')
        .get()
        .then(snapshot => {
          res.send(
            snapshot.docs.map(doc => {
              const data = Object.assign({ doc_id: doc.id }, doc.data());
              return data;
            })
          );
        })
        .catch(err => {
          res.send({ message: 'Something went wrong!', success: false });
        });
    })
    .catch(err => {
      res.send({ error: 'Something went wrong!', success: false });
    });
};

Firestore 规则:

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

      match /users/{users} {
    allow read: if request.auth != null;
      }
      match /discover/{discover} {
    allow read: if request.auth != null;
      }
      match /favorites/{favorite} {
    allow read, write: if request.auth != null;
      }
    }

}

我应该无法从 Postman 那里获取这些数据(因为我没有经过身份验证),但我仍然在获取这些数据。如果用户未登录,我不希望数据可以访问。

【问题讨论】:

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


    【解决方案1】:

    当您通过 Admin SDK 访问数据库或使用服务帐户的任何其他时间时,安全规则不适用。使用邮递员(或任何其他 HTTP 客户端)完全没有关系。此处实际进行数据库访问的是 Admin SDK。

    【讨论】:

    • 非常感谢!您对如何在没有 Admin SDK 的情况下读取和写入 Firestore 有什么建议吗?
    • 使用移动应用中的客户端库以及使用 Firebase 身份验证登录的用户。这是request.auth 起作用的唯一方法。
    猜你喜欢
    • 2023-02-09
    • 2020-08-24
    • 2020-02-24
    • 1970-01-01
    • 2018-12-02
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    相关资源
    最近更新 更多