【问题标题】:Security rules for Firestore DBFirestore 数据库的安全规则
【发布时间】:2018-04-17 17:40:17
【问题描述】:

我试图只允许用户编写 ID 与其电子邮件地址相同的文档。我似乎无法让这条规则起作用。我哪里错了?用户已通过身份验证,并在用户帐户上设置了电子邮件地址。

规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow write: if request.auth.email == request.resource.id;
    }
  }
}

呼叫:

firestore
    .collection('my-document')
    .doc(this.$firebase.auth().currentUser.email)
    .set({
      body: JSON.stringify(object),
      email: this.$firebase.auth().currentUser.email,
      submitted: new Date()
    })
    .then(function (docRef) {
      console.log('Successfully Written to DB.')
    })
    .catch(function (error) {
      that.isSubmitting = false
      console.error('Error adding document: ', error)
    })

错误:

Error adding document:  Error: Missing or insufficient permissions.
    at new FirestoreError (error.js?6b3a:140)
    at eval (webchannel_connection.js?80bc:250)
    at W.eval (webchannel_connection.js?80bc:195)
    at Ab (index.js?0dfd:23)
    at W.g.dispatchEvent (index.js?0dfd:21)
    at Re.Ca (index.js?0dfd:98)
    at ye.g.Oa (index.js?0dfd:86)
    at dd (index.js?0dfd:42)
    at ed (index.js?0dfd:39)
    at ad (index.js?0dfd:37)

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    事实证明,个人资料详细信息包含在令牌对象中,可以通过以下方式访问:

    request.auth.token.email
    

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 1970-01-01
      • 2021-07-12
      • 2021-04-11
      • 2018-10-19
      • 2019-07-05
      • 1970-01-01
      • 2020-05-17
      • 2021-09-27
      相关资源
      最近更新 更多