1

I am trying to access an authenticated users document and add a parameter to the document within the "users" collection if the uid matches.

The error message is as follow:

Error Domain=com.firebase Code=1 "Permission denied" UserInfo={NSLocalizedDescription=Permission denied}.

Whilst the error suggests it is a permission issue, when I change the child collection to an invalid path, the same error shows. I have also changed the rules to allow read and write regardless of authentication and the same error is shown.

Code to save the data:

//save the user data to firebase
func saveData(){

    //check if user is signed in
    guard let userID = Auth.auth().currentUser?.uid else {

        return

    }

    ref = Database.database().reference()

    self.ref.child("users").child(userID).setValue(["name": userName) {
      (error:Error?, ref:DatabaseReference) in
      if let error = error {
        print("Data could not be saved: \(error).")
      } else {
        print("Data saved successfully!")
      }
    }
}

Database rules:

service cloud.firestore {
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userId} {
  allow read, update, write, delete: if request.auth.uid == userId;
    }
}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ryan Hampton
  • 319
  • 1
  • 4
  • 21
  • The security rules you're showing apply to Cloud Firestore, while the code you're showing is accessing the Realtime Database. While both databases are part of Firebase, they're completely separate, and the security rules for one don't apply to the other. To fix the error, you will have to set the rules for the Realtime Database. For a walkthrough of how to do that, see https://stackoverflow.com/a/52129163 – Frank van Puffelen Jun 07 '20 at 15:25

0 Answers0