9
I created the following security structure in Realtime Database:
{
"rules": {
"receita": {
"$chave": {
".write": "auth.uid === newData.child('usuario').val()",
".read": "auth.uid === data.child('usuario').val() || data.child('tipo').val() === true"
}
}
}
}
For the following database structure:
{
"receita" : {
"-L92JgTg4xIDCymtq26h" : {
"imagem" : "default",
"ingrediente" : [ "teste" ],
"nome" : "teste",
"preparo" : "teste",
"tipo" : true,
"usuario" : "Az2Mra1xJwXtpaspGwgvj7hm9NA2"
},
"-L9CwBmuVETG5TSjLHz7" : {
"imagem" : "default",
"ingrediente" : [ "teste" ],
"nome" : "teste",
"preparo" : "teste",
"tipo" : true,
"usuario" : "Az2Mra1xJwXtpaspGwgvj7hm9NA2"
}
}
}
Using Authentication with Google Provider
The ".write"
works perfectly, however the ".read"
no (perm error), I already changed it to:
".read": true //Leitura pública
But it didn’t work either, I believe the error lies in the structure of my rule but I don’t know where
To "work," I changed the rules to:
{
"rules": {
"receita": {
"$chave": {
".write": "auth.uid === newData.child('usuario').val()"
},
".read": "true"
}
}
}
I tested so too, but unsuccessfully:
".read": "auth.uid === data.child($chave + '/usuario').val() || data.child($chave + '/tipo').val() === true"
With the rules structured in the first way, I tried to fetch the data as follows:
firebase.database().ref('receita').once('value').then(function(snapshot) {
console.log(snapshot.val())
})
However it results in the permission error, but when doing so passing a valid key to reference works:
firebase.database().ref('receita/-L8UX8Ajz8P1XcudJUih').once('value').then(function(snapshot) {
console.log(snapshot.val())
})
So the error is picking up a reference from the bank because in the recipe directory (/receita
), I have no rule, only in /receita/$chave
You have the rule
auth.uid === newData.child('usuario').val()
, but in your bank, I see you have the user-L7WxcAHr8LkfJAiI8ku
. This id seems to have been generated by apush()
in the database. Theauth.uid
has a different value than this push. So the rule will always returnfalse
.– Rosário Pereira Fernandes
@Rosáriopereirafernandes no, the value
-L7WxcAHr8LkfJAiI8ku
is generated by Authentication, use push only to add a recipe object key. the method that defines the user key:auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()).then(function(result) { chave_usuario = result.user.uid; })
– Costamilam
Very well... but what you’re trying to do seems contradictory. Its rules allow reading only recipes created by a user and true type (there is a filtering). But you want the rules to allow you to read all recipes (no filtering).. Are you trying to do queries for 2 types of user? The "admin" reads all recipes and the normal user reads only his recipes?
– Rosário Pereira Fernandes
The idea is: Users can see their revenues (public or private); Users can see public revenues (
tipo === true
); I have no admin or normal user, only users– Costamilam
Is not "Its rules allow reading only recipes created by a user and of the true type", and yes "Its rules allow reading only recipes created by the connected user OR true-type"
– Costamilam