简体   繁体   中英

how to set permissions in firebase firestore

    $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

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

One of the most common 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;
  }
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM