Examples for Authenticate and Insert Data in DB (UNITY)
I am using Firebase Unity and trying to authenticate a user and then insert data into the DB. But eveytime I do this it says Permission Denied. My Rules are as follows:
{
"rules": {
"read" : "auth != null",
"write": "auth != null"
}
}
i am able to sign in anonymously but when I try to write into the DB, I am getting a permission denied error.
If there are any examples available on how to achieve this then please if you point me to the right resource.
I think I see the problem - you're using "read" and "write" as properties to describe the rules you want, but the keywords you want are actually ".read" and ".write". (Note the period at the front!) You should be able to fix this problem by just adding a "." to the front of your lines, to get something more like this:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
What's happening is that it's interpreting it as you trying to set the rules for a directory named "read". (And then it's hitting an error, since it expects directories to be described in the form of dictionaries, listing the .read and .write properties.)
If you want an easy way to test and debug Firebase database rules, I highly recommend fiddling with the simulator, in the firebase console. On the page where you set database rules, on the right-hand-side, there is a simulator where you can make mock queries to the database, and it will tell you if they succeed or not, and what part of the permissions descriptor applied. It's super-useful for debugging this sort of problem!
Hope this helps!
Sorry, my rules have .read/.write.
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"myDB":{
"myStore":{
".indexOn": ".value"
}
},
"my-queue":{
"tasks":{
".indexOn": "_state"
}
},
"watcher":{
"tasks":{
".indexOn": "_state"
}
}
}
}
Still, I am not able to write. Is there any tutorial which explains to process of authenticating and then adding data to Realtime DB for unity, because till now i haven't been able to find one.
As, I said, i am able to sign in anonymously and when I write it gives me permission denied.
Dang! I was hoping this would be an easy question. :-P
So, your rules look fine, as written. (I tried copying them into the simulator and running some tests just to make sure, but everything looks good.) So I don't think the problem is your rules.
I do have a few more ideas, though, so hopefully one of these might hit!
-
The Firebase Auth plugin currently only works on mobile platforms. (Android or iOS) If you are running it from a desktop environment (either a desktop build, or just running directly in the unity editor) then you need to add some code to allow the editor to simulate an authorized account.
-
If your rules are working, but it's still not authorizing, then double check to make sure that you are pointed at the correct database. Verify that you have included the
google-services.json(Android) orGoogleService-info.plist(iOS) file in your assets directory somewhere, and that it points to the correct database. (Just open it in a text editor - it's in plaintext, so you can see the URL.)
Let me know if one of these ends up helping!
Thanks for the input. Yes, i was running a PC Standalone, therefore the auth issue. But when I ran in android, it worked like CHARM!
Awesome, glad to hear it!