Security rules for Firestore DB

2019-08-20 19:04发布

I'm trying to only allow users to write documents where the ID is equal to their email address. I can't seem to get this rule to work. Where am I going wrong? The user is authenticated, and email address is set on the user's account.

Rule:

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

Call:

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:

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)

1条回答
相关推荐>>
2楼-- · 2019-08-20 19:31

Turns out that profile details are contained in the token object, and can be accessed as follows:

request.auth.token.email
查看更多
登录 后发表回答