【问题标题】:Why my permissions for firestore don't work?为什么我的 Firestore 权限不起作用?
【发布时间】:2020-04-21 08:13:44
【问题描述】:

我的代码

created(){
let ref = db.collection('messages').orderBy('timestamp')

// subscribe to changes to the 'messages' collection
ref.onSnapshot(snapshot => {

  snapshot.docChanges().forEach(change => {
    if(change.type == 'added'){
      let doc = change.doc
      this.messages.push({
        id: doc.id,
        name: doc.data().name,
        content: doc.data().content,
        timestamp: moment(doc.data().timestamp).format('lll')
      })
    }
  })
})

我的 firebase 规则:

service cloud.firestore { match /databases/{database}/documents {
 match /messages/{document=**} {
  allow read, update, delete: if request.auth.uid == resource.data.uid;
  allow create: if request.auth.uid != null;
 }
}}

我的错误

onSnapshot 中未捕获的错误:FirebaseError:缺失或不足 权限。

帮助解决问题

【问题讨论】:

  • 您是否已登录或已通过身份验证?由于您的规则 allow read, update, delete: if request.auth.uid == resource.data.uid 会阻止您的请求,因此会发生此错误。
  • 我试过这个,同样的问题:service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read: if true;允许写入:如果为假; } } }
  • 提升它的实际效果
  • 你确定this.messages代表messages集合吗?

标签: javascript firebase vue.js google-cloud-firestore firebase-security


【解决方案1】:

查看文档中的Rules are not filters

您只允许满足request.auth.uid == resource.data.uid 的读取操作,但您正在尝试读取整个db.collection('messages') 集合。

尝试阅读db.collection('messages').where('uid', '==', currentUserUid)

(并且不要忘记在消息中实际放置 uid 属性。)


PS:您的规则看起来像是用于个人笔记,而不是用户之间的消息。如果您希望收件人与作者不同,则必须更改数据结构和规则。也许有 authorUidrecipientUid 字段,我不知道,我还没有在 Firestore 中做过类似的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-01
    • 2020-09-12
    • 2019-02-05
    • 2021-09-02
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2020-11-17
    相关资源
    最近更新 更多