【问题标题】:Prevent guest users from writing to Cloud Firestore database in Firebase阻止来宾用户写入 Firebase 中的 Cloud Firestore 数据库
【发布时间】:2018-01-26 20:41:48
【问题描述】:

我有一个类似 Instagram 的应用程序。我想实现访客用户身份验证,因此人们不必注册即可查看其他人的照片。

但我还需要为来宾用户禁用所有写入数据库的功能。我怎样才能做到这一点?

假设我有这个默认的安全规则:

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

我知道我不应该在现实生活中这样做。仅作为示例。

【问题讨论】:

标签: firebase firebase-authentication google-cloud-firestore


【解决方案1】:

您所询问的内容几乎直接针对in the docs,但看起来类似于:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches the 'author_id' field
    // of the document
    match /photos/{photoId} {
      allow read: if true;
      allow create: if request.auth.uid != null && request.data.uid == request.auth.uid;
    }
  }
}

这将允许任何经过身份验证的用户创建照片,只要uid 字段与他们自己经过身份验证的uid 匹配,但将允许任何人(无论是否登录)读取photos 集合中的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2018-06-22
    • 2018-08-03
    • 2018-06-17
    • 1970-01-01
    相关资源
    最近更新 更多