【问题标题】:Issue in authentication in firebase cloud firestore at user level用户级别的 Firebase Cloud Firestore 中的身份验证问题
【发布时间】:2019-01-09 16:15:13
【问题描述】:

我正在分析 Firebase Cloud Firestore 的入门指南。这是一个安卓应用程序。 https://firebase.google.com/docs/firestore/security/get-started

我需要从每台安卓设备上保存和读取用户数据以备份一些数据。 firebase cloud firestore 的文档显示了以下代码,用于访问所有用户的数据。但是我想将用户数据限制为私有,以便只能由保存它的用户访问。请建议如何进行访问/身份验证过程。

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
    match /databases/{database}/documents {
        match /{document=**} {
            allow read, write: if request.auth.uid != null;
        }
    }
}

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    如果您在文档中将保存文档的用户的 uid 存储在名为(例如)createdBy 的字段中,您可以执行以下操作:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read: if request.auth.uid != null && resource.data.createdBy == request.auth.uid;
          allow write: .... 
        }
      }
    }
    

    【讨论】:

    • 我们还能看到控制台中的数据吗?
    • 我明白你的回答.. 它只对创建者可见吗?
    • 确实如此。如果您在创建时将作者的 uid 存储在字段 createdBy 中,allow read 的规则将只读权限仅限于作者。您可以添加规则以避免在创建后对该字段进行任何修改(例如在allow update 规则中)。我建议您观看有关该主题的官方视频:youtube.com/watch?v=eW5MdE3ZcAw。如果您认为我的回答有帮助,您可以点赞并接受。谢谢。
    • 最后一个问题.. 我们仍然可以在控制台中查看每个人的数据吗?我不想在控制台中看到用户数据。您提供的代码 sn-p 会处理这个问题吗?
    • request.auth.uid 中,您将在身份验证部分拥有用户 ID,即您在 Firebase 控制台中看到的用户 ID。有关详细信息,请参阅firebase.google.com/docs/auth。除了您的接受之外,别忘了给我的答案投票!谢谢!
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2020-05-22
    • 2022-10-15
    相关资源
    最近更新 更多