Firebase Firestore:写入权限不足

问题描述 投票:1回答:1

我正在尝试写一份文件,但是得到了

权限丢失或不足。

我的规则如下:

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

我想写

const accountBalanceRef = db.collection('accountBalances').doc()
accountBalanceRef.set({
    type: s.type,
    accountName: s.accountName,
    startingBalance: s.startingBalance,
    endingBalance: s.endingBalance,
    statementYear: s.year,
    statementMonth: s.monthNumber,
    createdAt: now,
    uid: this.props.uid
})

怎么了? this.props.uid是我的用户名

解决:

在@Bob Snyder的帮助下,我用以下规则解决了这个问题

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

我需要有不同的读写规则。我认为这是因为对于阅读,我并没有确切地设置request.resource.data.uid

firebase google-cloud-firestore
1个回答
3
投票

在Firestore安全规则中,resource.data是数据库中文档的值。您的规则不允许创建accountBalance文档,因为resource.data为null。

如果您希望规则要求accountBalance中的uid字段等于授权用户的uid,则规则应使用request.resource.data.uid

allow read, write: if request.auth.uid == request.resource.data.uid;

request.resource的文档解释说:

[request]资源变量包含有关正在编写的文档的数据和元数据。它与resource变量密切相关,qazxswpoi变量包含所请求路径中的当前文档,而不是正在编写的文档。

© www.soinside.com 2019 - 2024. All rights reserved.