Search code examples
fluttergoogle-cloud-firestorefirebase-authenticationfirebase-security

How define permission on Firebase


I'm having permission problems when I'm trying to do a query on Firebase Firestore.

This is the code that performs the query.

Future<void> fetchData() async {
    FirebaseFirestore db = FirebaseFirestore.instance;
    CollectionReference diariosRef = db.collection("diarios");
    QuerySnapshot feveireiro = await diariosRef
        .where("data_publicacao",
            isGreaterThanOrEqualTo: DateTime(1, DateTime.november, 2023))
        .get();
    for (var doc in feveireiro.docs) {
      print(doc.data());
    }
  }

And i get this error:

E/flutter ( 7158): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.

And this is how the permissions rules are set up in my Firebase:

rules_version = '2';

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

Im also using Cloud Storage and Firebase Auth, but Cloud Storage has the same rules.

What is wrong?


Solution

  • Your rules should allow for any access but if you wanted to specifically add permissions to diarios you could try:

    rules_version = '2';
    
    service cloud.firestore {
      match /databases/{database}/documents {
        match /diarios/{document=**} {
          allow read, write: if true;
        }
      }
    }