【问题标题】:Insufficient permissions clientside while simulator works模拟器工作时客户端权限不足
【发布时间】:2018-06-25 17:43:03
【问题描述】:

我有以下 Firestore 规则设置,我想让任何人在建议集合中创建文档,但只有特定用户可以更新它。规则如下所示:

  service cloud.firestore {
    match /databases/{database}/documents {
      match /suggestions/{sugg} {
        allow create, read;
        allow update: if request.auth.uid == 'abc123';
      }
    }
  }

当我在提供的模拟器中测试它时,它工作正常;但是在等待 30 分钟后,我在部署的应用程序中进行了测试,但出现了错误:

错误:权限缺失或不足。

在我的应用程序中,我正在对建议集合进行add() 调用。所以在我指定allow create 的规则中,这就足够了。我添加了read,以防返回的文档被视为已读。

客户端:(AngularFire)

this.afs.collection('suggestions').add(sugg).then(() => {
  this.submitted = true;
}, err => console.error('Firebase error:', err));

【问题讨论】:

  • 请添加重现问题的相关客户端代码。
  • @DougStevenson 你去吧

标签: firebase google-cloud-firestore angularfire2 firebase-security


【解决方案1】:

问题是当 request.auth 为 null 时,Firestore 无法尝试匹配 uid。解决方案是添加一些功能:

 service cloud.firestore {
    match /databases/{database}/documents {
      function isSignedIn() {
        return request.auth != null;
      }
      function isAdmin() {
        return isSignedIn() && request.auth.uid == 'abc123';
      }

      match /suggestions/{sugg} {
        allow create, read;
        allow update: if request.auth.uid == 'abc123';
      }
    }
  }

【讨论】:

    猜你喜欢
    • 2020-07-10
    • 2021-10-19
    • 1970-01-01
    • 2015-11-25
    • 2021-04-22
    • 2022-12-08
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多