Good morning!
I am working on a Firebase project in my Unity game and am working on securing the data in the database, however whenever I update the rules, it breaks my (otherwise working) handlevaluechanged function.
The data structure looks like this:
User_List -> Firebase_User_ID -> Health: 100, Name: Foo, etc ....
I want the rules (at a minimum, I'll add validation later) for this data to be:
"USER_LIST":
{
"$UID" :
{
".read": "auth.uid === $UID",
".write" : "auth.uid === $UID",
}
},
Locally in the game, I get a reference with this call:
FirebaseDatabase.DefaultInstance.GetReference("USER_LIST").Child(USER_ID).ValueChanged += HandleValueChanged;
If I set the rules to:
".read": true,
".write" : true
everything works as expected. The client can update the database, and handlevaluechanged does its job of keeping the local data synced. When I switch the to the rules above where I verify the auth ID, the client still works. It can update the database no issues, provided the correct user ID is signed in. However, my Handlevaluechanged gets a permission denied error from firebase, as if the handlevaluechanged listener does not supply the proper user ID when it attempts a read from the DB.
I'm puzzeled because the rules allow me to get the reference in the first place, and update the database from the client, but I can't update the client from the database? What am I missing?
I have also tried to GetReference at the USER_LIST node instead of the USER_ID node with the same result.
Any insight would be greatly appreciated! Thank you in advance.
AuthStateChangedevent handler? You'll need to ensure that you connect and disconnect the handler whenever the user state is updated. If it's like the other SDKs, the current user will be considered "pending" (pretty much treated asnull) until the user's authentication state is confirmed. In this short window, the above rules would throw the permission error. - samthecodingmanAuthStateChangedevent handler will fire indicating that auth is ready to use. - samthecodingmanwindow.onload = doSomething();, but for the Firebase authentication service. - samthecodingmanRulestab next toData). I play with it a little in this article for Cloud Storage, but the functionality is basically the same for Realtime Database: medium.com/firebase-developers/… . You can also assert thatFirebaseAuth.DefaultInstance.CurrentUser != nullif you want a quick fuzz test to see if that's your issue. - Patrick Martin