Not able to access a firebase collection after publishing security rule

Viewed 40

I was trying to access data from Cloud Firestore, but the access is denied.

My flutter code

FirebaseAuth _auth = FirebaseAuth.instance;
    final logineduser = _auth.currentUser!.uid;
    final QuerySnapshot result = await FirebaseFirestore.instance
        .collection('product_details').get();
final List<DocumentSnapshot> documents = result.docs;
print(documents[0]);

The Cloud Firestore Security rules

rule_version='2'
service colud.firestore{
   match /databases/{database}/documents {

     match /user_details/{emailId}{
        allow read,write;
     }

     match /user_activities/{userId}{
        allow read,write;
     }

     match /product_details/{Id}{
        allow read,write;
     }

     match /purchase_details/{Id}{
        allow read,write;
     }

     match /{document=**}{
        allow read,write:if request.auth.uid !=null;
     }
  }
}
1 Answers

This says to allow read or write if the authentication is not null.. So unless the user is authenticated you will not be able to access data except for the queries you have written above this. You can either implement login or to make it public you can write

allow read,write;

Please note it is NOT a good practice to keep it public. Maybe for learning you can try this.

Related