Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase permission-denied

I'm a newbie in firebase.

How do I get through this below rule?

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

I've tried to change the rule to below,

{
  "rules": 
   {
    ".read": true,
    ".write": true,
   }
}

but there's an error of

mismatched input '{' expecting {'function', 'service', 'syntax'}

Below is the db structure.

db structure

Currently, this is my code (which keeps returning Permission-denied):

    // [START initialize_database_ref]
    mDatabase = FirebaseDatabase.getInstance().reference
    // [END initialize_database_ref]

    val result = HashMap<String, Any> ()
    result.put("timestamp", getCurrentTime())
    result.put("user_id", USER_ID)
    result.put("x_position", -1)
    result.put("y_position", -1)

    mDatabase!!.ref.child("raw data").child("Z9QD79lDzP7MpD6feWeJ").setValue(result).addOnFailureListener(object : OnFailureListener {
        override fun onFailure(@NonNull e: Exception) {
            Log.d("firebase", e.localizedMessage)
        }
    })

Any help would be appreciated! Thanks :)

like image 318
user3415167 Avatar asked Dec 26 '17 21:12

user3415167


People also ask

What are the restrictions in firebase?

Firebase Security Rules work by matching a pattern against database paths, and then applying custom conditions to allow access to data at those paths. All Rules across Firebase products have a path-matching component and a conditional statement allowing read or write access.


2 Answers

I was able to solve the problem by switching from Cloud Firestore to Realtime Database, and then changing the rule. (After I've changed this, there was no more error showing!) enter image description here

like image 118
user3415167 Avatar answered Oct 11 '22 13:10

user3415167


For others struggling with this problem, the proper solution is to change the false in

service cloud.firestore { 
    match /databases/{database}/documents {
        match /{document=**} { 
            allow read, write: if false; 
        } 
    }
}

To true while testing. Don't forget to add additional security when going live with your project!

like image 41
Nathan Bird Avatar answered Oct 11 '22 13:10

Nathan Bird