简体   繁体   中英

Realtime database authentication: failed: DatabaseError: Permission denied

I would like to use these rules from the security quickstart guide

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

Although I do not understand where the auth.uid is read from?

I can get it to store information when I do the below so I can confirm the problem is with auth.id . I'm not sure if I need to pass that as a parameter or something? I'm afraid this is where I am lost.

{
  "rules": {
    "users": {
      "$uid": {
        ".read": true,
        ".write": true
      }
    }
  }
}

This is the current java code I am using to test it with:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
User u = new User("Jack", "jack@email.co.uk", "12345678"); // Very simple class holding this user information
myRef.child("users").child(mFirebaseUser.getUid()).setValue(u);

I've been looking at information on firebases real time database but every time they get to the authentication they just say "For now we will turn off authentication but make sure you use it in production!".

Well, you don't need to pass as a parameter, firebase do it for you. Let me try to explain, after you create a user in your console and login in your application, firebase will provide each user with a different uid (similar to this ca07912b-7e65-41ad-8342-e87cf6f6ceb2 ).

So after you login it will be stored in your smartphone and every request that you do in the database firebase will analyse your security rules, so when you are saying that only the owner of the uid X can access /users/**X**/.. then if you are not logged in you don't have permission to read neither to write on this path.

I'm a bloody idiot,

I was calling FirebaseAuth.getInstance().signOut(); a few lines above my code which I originally put in for debugging purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM