【问题标题】:how to set permissions in firebase firestore如何在firebase firestore中设置权限
【发布时间】:2019-01-23 05:30:58
【问题描述】:
    $path = "firebase_auth.json";

    $config = array(
        "projectId" => "XXXX",
        "keyFile" => json_decode(file_get_contents($path), true)
    );
    $firestore = new FirestoreClient($config);
    $collectionReference = $firestore->collection('Channels');
    $snapshot = $collectionReference->documents().get();

此代码的响应是

遇到未捕获的异常

类型:Google\Cloud\Core\Exception\ServiceException

消息:{ "message": "缺少或权限不足。", "code": 7, "status": "PERMISSION_DENIED", "details": [] }

文件名:/var/www/html/riglynx/vendor/google/cloud/Core/src/GrpcRequestWrapper.php

行号:263

【问题讨论】:

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


【解决方案1】:

查看Get started with Cloud Firestore Security Rules 文档。请参阅Writing conditions for Cloud Firestore Security Rules 文档。

最常见的安全规则模式之一是控制访问 基于用户的身份验证状态。例如,您的应用可能 希望只允许登录用户写入数据:

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to access documents in the "cities" collection
    // only if they are authenticated.
    match /cities/{city} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

这应该可以帮助您入门。

【讨论】:

  • 我需要完全访问所有文档而不是特定文档
  • 点击 ALLOW ALL 获取要粘贴的确切代码。
【解决方案2】:

您收到此错误的原因是您无权访问名为 Channels 的集合的文档。

为了解决此问题,您必须登录控制台 Firebase。 导航到数据库 > 在 firestore 数据库下,您必须单击规则。

根据规则,您可以根据自己的意愿给予许可。 如果您想授予所有用户访问权限,那么您可以简单地将当前代码替换为以下代码。 (不是一个好的做法,也不安全)

service cloud.firestore {
 match /databases/{database}/documents {
  match /{document=**} {
    allow read, write;
  }
 }
}

【讨论】:

  • service cloud.firestore { match /databases/{database}/documents { match /{document=**} { 允许读取,写入:如果为真; } } } 我也设置了
  • 你能确保你使用的是正确的数据库吗?如果是,您也可以从模拟器中进行测试。您可以在规则选项卡中找到模拟器。您可以尝试开启和关闭身份验证并查看响应。
  • 如果模拟器出现错误,那么您需要重新检查您的规则。复制并粘贴上面给定的代码,然后重试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
  • 2022-01-25
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多