最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to set permissions in firebase firestore - Stack Overflow

programmeradmin3浏览0评论
    $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();

Response of this code is

An uncaught Exception was encountered

Type: Google\Cloud\Core\Exception\ServiceException

Message: { "message": "Missing or insufficient permissions.", "code": 7, "status": "PERMISSION_DENIED", "details": [] }

Filename: /var/www/html/riglynx/vendor/google/cloud/Core/src/GrpcRequestWrapper.php

Line Number: 263

    $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();

Response of this code is

An uncaught Exception was encountered

Type: Google\Cloud\Core\Exception\ServiceException

Message: { "message": "Missing or insufficient permissions.", "code": 7, "status": "PERMISSION_DENIED", "details": [] }

Filename: /var/www/html/riglynx/vendor/google/cloud/Core/src/GrpcRequestWrapper.php

Line Number: 263

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 23, 2019 at 5:30 Keyur NimavatKeyur Nimavat 3,6353 gold badges15 silver badges19 bronze badges 1
  • It looks like you don't have permission to read Channels. See firebase.google./docs/firestore/security/get-started. The simplest (but least locked down) version is in the ALLOW ALL box of the second example on that page. – Frank van Puffelen Commented Jan 23, 2019 at 5:33
Add a ment  | 

2 Answers 2

Reset to default 3

check out Get started with Cloud Firestore Security Rules documentation. And see Writing conditions for Cloud Firestore Security Rules documentation.

One of the most mon security rule patterns is controlling access based on the user's authentication state. For example, your app may want to allow only signed-in users to write data:

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;
    }
  }
}

This should help you get started.

Reason you are getting the error is because you are not allowed to access documents of the collection called Channels.

In order to fix this, you have login to your console firebase. Navigate to Database > Under firestore database, you have to click on Rules.

Under rules, you can give permission as per your wish. If you want to give access to al the users then you can simple replace current code with the following code. (Not a good practice and not secure too)

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

评论列表(0)

  1. 暂无评论