Error Domain=FIRFirestoreErrorDomain Code=7 "Missing or insufficient permissions."

Viewed 994

So I have the following code retrieving documents from firestore, this function is called in my viewDidLoad():

func loadAnnotations() {
    
    db.collection("jumpSpotAnnotations").getDocuments { (querySnapshot, error) in
        if let error = error {
            print("There was an issue retrieving data from Firestore: \(error)")
            
            let alertController = UIAlertController(title: "Oops!", message: "We are having trouble loading the cliff jumping locations.  Be sure to check your internet connection.", preferredStyle: .alert)
            
            let okayAction = UIAlertAction(title: "Okay", style: .cancel, handler: nil)
            
            alertController.addAction(okayAction)
            
            self.present(alertController, animated: true, completion: nil)
        } else {
            if let snapshotDocuments = querySnapshot?.documents {
                //print(snapshotDocuments)
                for doc in snapshotDocuments {

The rest of the function isn't important. I am getting an error when retrieving the data: Error Domain=FIRFirestoreErrorDomain Code=7 "Missing or insufficient permissions." When I first got this, I realized that it was due to the fact that I had not updated my security rules since the first day that the Firebase project for the app was created, and 30 days has passed, so Google wouldn't access anymore until I updated the rules. These are the ORIGINAL rules:

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

For the purpose of this app, right now I am fine with user creating and reading any data. I do not, however want them to be able to modify or delete data. So I asked a question here on Stack about how to do that, and was answered here: How to prevent Firestore documents being deleted / modified, but allow all reads and writes So my updated security rules are now:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /collection1/{jumpSpotAnnotations} {
      allow read, create;
    }
  }
}

However I am still getting the exact same error when I try to retrieve the data. The new security rules HAVE been published. So my question is, is there something I need to do after pressing publish to update it officially, and have my simulator be able to retrieve the data, OR is the problem that my code in either my security rules or my function in Xcode is incorrect for what I want to do. Keep in mind, the function in Xcode worked perfectly with the original rules (I made the rules completely open like that when I first made the Firestore project so I could just test in the simulator easily). Any help is much appreciated!

1 Answers

In my answer to your previous question, I suggested that you call out the names of the individual collections. You just took what I gave and copied it verbatim. You need to use the name of the actual collection in your rule:

    match /jumpSpotAnnotations/{id} {
      allow read, create;
    }

Again, I strongly recommend that you read the documentation I linked to so you can learn how security rules work.

Related