1
votes

I am trying to read the data that was assigned to a specific user by its uid value,but I get permission denied.

Anyone knows the reason why I get this error?

import UIKit
import Firebase

  class ViewController: UIViewController {
  // dataChild value  == User UID as in console.firebase.google.com
 var dataChild = "xd5rwZzUqoRbfMp2rq5pTRB3s1"
    var dbRef:FIRDatabaseReference! // reference to Firebase

 override func viewDidLoad() {
      super.viewDidLoad()
       dbRef = FIRDatabase.database().reference().child("Users")
           startObservingDB()
}
func startObservingDB() {
    dbRef.child(dataChild).observe(.value, with: { (snapshot: FIRDataSnapshot) in
        print("The snapshot is \(snapshot.value)") //no longer prints

    }, withCancel: { (Error:Any) in
        print("Error is \(Error)") //prints Error is Error Domain=com.firebase Code=1 "Permission Denied" 
    })
  } // end of startObservingDB()

}

uid for the corresponding email address that was used to register account

Firebase Json Tree

{
"Users" : {
   "xd5rwZzUqoRbfMp2rq5pTRB3s1" : {
       "718565122" : {
        "BookingAmount" : "12",
        "BookingNumber" : "718565122",
        "DateAndTime" : "Mon, 26 Sep 2016 18:30",
        "EmailAddress" : "[email protected]",
        "FlatNumber" : "10",
        "FrequecyAmount" : 48,

Firebase Rules

{
  "rules": {
     ".read": "auth != null",
     ".write": "auth != null"
  }
}
1
This means you don't have permission to read the data. Read the Firebase documentation about setting up security rules for your app to learn all about that: firebase.google.com/docs/database/security.Frank van Puffelen
@FrankvanPuffelen Each time I save data in the db it is of form Users/Uid /first node of data e.g 718565122. Uid is the ID of the user obtained through Firebase Authentication. Given the fact that I am using a valid uid in my path, firebase should consider that the user is authenticated and allow me to read its data. I have update my question with the security rules too.bibscy

1 Answers

4
votes

I had forgotten to add the pods for pod 'Firebase/Auth' and import FirebaseAuth in my swift file. This is the reason why I could not read the firebase database with a valid uid.