Firebase Database index : information and effects - firebase-realtime-database

I m using firebase Database .Planning to have quite a large data collection , I'm trying to use indexes . Making some tests, i don t know if rules i implement are correct let alone improve the queries .Is there a way to have a feedback on those indexations (correct or not, improve or not)
right now i have one class as described in the picture and i have the created the following rule
{
"rules": {
".read": true,
".write": true,
"users": {
"$user_id": {
".indexOn": ["user_id", "username"]
}
},
}
Since i get no feedback from firebase, i don t know if this is correct and improves anything .

Firebase's server-side rules are meaningless without seeing the code that exercises them.
But at a first glance it seems you're defining your indexes too low in the tree: indexes need to be defined at the location where you run the query. So if you want to run a query on /users, you need to define the indexes on /users:
{
"rules": {
".read": true,
".write": true,
"users": {
".indexOn": ["user_id", "username"]
}
}
}

Related

Realtime Database Rules not working as expected

I am facing a puzzling situation with Realtime Database Rules.
The rules below are working, they allow me to write data to FirstCollection:
{
"rules": {
"FirstCollection": {
".read": true,
"$Section": {
".write": "(auth!=null)"
}
}
}
}
This second set of rules used to work until yesterday, and it no longer works:
{
"rules": {
"FirstCollection": {
".read": true,
"$Section": {
".write": "(auth!=null)&&((auth.uid=='98ab..myOwnUID..23YZ')||(auth.uid=='98ab..aSecondUID..23YZ'))"
}
}
}
}
I have checked everything I could think about including the exact value of myOwnUID.
Can any experienced user spot the issue or any mistake I could be making (or could have made)?
Here is the issue I was having. The rules have in fact nothing wrong.
What happens is that the code in my app is creating a new user with the auth.createUserWithEmailAndPassword API function. And since this function has the "annoying side effect" of signing in as the newly created user, when I then hit the rules in the flow of the app my auth.uid is no longer what I expect it to be, thus explaining why the rules fires.

How can I ban a User in Firebase realtime Database using the rules?

I've created a list of banned users in my realtime Database and I want to make them impossible to log-in, I know I can use the database rules, but I don't know how, can someone help me?
This is my database structure:
/banned-users:
/UserId1:True
/UserId2:True
Those are my database rules:
{
"rules": {
".read": true,
".write": true
}
}
You need to reference data in other paths as follows:
{
"rules": {
"thepath": {
".read": "root.child('banned-users').child(auth.uid).val() !== true"
".write": "root.child('banned-users').child(auth.uid).val() !== true"
}
}
}
Note that actually you don't "make them impossible to log-in", because this is not possible, but you prevent them writing to/reading from your database.

Error saving rules - Line 5: Expected '{'

{
"rules": {
".read": true,
".write": true,
".delete": true
}
}
these rules show me error Error saving rules - Line 5: Expected '{'.
Please help
Firebase Realtime Database rules don't support a ".delete" clause, and that's the reason you get this error message. Correct rules are:
{
"rules": {
".read": true,
".write": true
}
}
These rules will also allow deleting of the data, as part of the .write rule.
If you want to later disallow deletes or other types of updates, have a look at the documentation on new data vs existing data.

Using an unspecified index. Consider adding ".indexOn": "phone" at /use_frameworks_beta_2/searchIndex to your security rules for better performance

I have an iOS app where I have to implement a simple chat between two users. I am registering the users to Firebase during app registration. All work good. But when I have to start a chat thread I am searching for a user using search indexes but I am getting following error:
Using an unspecified index. Consider adding ".indexOn": "phone" at /use_frameworks_beta_2/searchIndex to your security rules for better performance
Using an unspecified index. Consider adding ".indexOn": "email" at /use_frameworks_beta_2/searchIndex to your security rules for better performance
Using an unspecified index. Consider adding ".indexOn": "name" at /use_frameworks_beta_2/searchIndex to your security rules for better performance
The Rules in Firebase console is as follows
{
"rules": {
".read": true,
".write": true,
"searchIndex": {
".indexOn": ["email","phone","name"]
}
}
}
Please help me fixing this error.
The .indexOn specifier must be placed in the rules at the location containing the indexed keys. Update your rules to place .indexOn at the correct location in the tree:
{
"rules": {
".write": true,
".read": true,
"use_frameworks_beta_2": {
"searchIndex ": {
".indexOn": ["email","phone","name"]
}
}
}
}

Firebase Nested Index Query Rules

Last night I was trying to index a nested object in firebase's database. Below is my object data and rules. Currently on orderedBy locID it is returning index not defined. Would appreciate any help thanks!
My Data:
My Rules:
{
"rules": {
"locations": {
"$uid": {
".indexOn": ["locID"]
}
},
".read": true,
".write": true
}
}
The code that triggers the error:
locData = db.child("Locations").order_by_child("locID").equal_to(someID).get()
The above is returning index not defined, using the pyrebase wrapper.
You're skipping a level in your query.
Firebase will query the immediate children of the location on which you execute the query. So in your case, it will query items 10S and 12S and try to order/filter on locID. Neither of these nodes has a child locID, since that property is one level deeper in the tree.
Pyrebase likely talks against the Firebase REST API, which will fail when there is no index for the field you're trying to order/filter on.
It's a bit hard to be certain for your use-case, but in general the solution to this problem is to keep a reverse index. In this case that could be a list that keeps track of the 10S/12S thing for each locID.
locByLocID: {
5689: "10S",
8223: "12S"
}

Resources