1
votes

I am having a database in Firestore which have the number of collections for which I have defined security rules with read, create, update and delete operations.

Till Saturday noon, all the rules with given condition was allowing to read and write the data to the collections but suddenly after that in one specific collection named "locations" it giving PERMISSION DENIED exception only for creating document in the same collection but read, update and delete works.

So I changed rule for that collection like for testing as below

match /locations/{locationID} {
    allow read: if true;
    allow create: if true; //Condition commented...
    allow update: if <condition>;
    allow delete: if false;
}

and in Android Client I have coded as

database.collection("locations").add(mapData)
            .addOnCompleteListener {
                 if (it.isSuccessful) {
                     //Success
                 } else {
                     //Exception
                 }
             }

Even though, making to allow create: if true, it gives the same exception of Missing or Insufficient Permission. but when I change it to "allow write: if true" it works and document is added

When I changed collection name to test in both Rules and Client Code, it creates new document in the same collection but when I changed collection name to locationData, location and locations, it won't work.

Is this an issue in Firestore rule or What can be the solution for this?

1

1 Answers

1
votes

It seems like your update rule is triggered while trying to save the dataMap to Firestore.

Please note that add() is basically a doc.set().

The behavior of set() is that it tries to overwrite the doc if it already exists and will perform an update operation thereby triggering the update rule in your security rules.

You have two options:

1) Check the data you're trying to save already exist in the locations collection.

2) Set the update rule to allow true so that you can verify that the document already exists.

Hope that helps.