I have a static web page application (no backend server), that uses Firebase as User's database (javascript client). The database has the following structure:
{
users:
{
$userId:{
///private user data
}
},
project:
{
$projectId:{
data:{...},
permissions:{
$userId:"owner"|"read"|"write"
}
}
}
}
And user only have access to his private data and projects where his Id is mentioned in project's permissions. Here is security RULES:
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
},
"project":{
"$projectId":
{
".write":"auth!=null && (!data.exists() || root.child('project/'+$projectId+'/permissions/'+auth.uid).val()==='owner')",
".read":"root.child('project/'+$projectId+'/permissions/'+auth.uid).val()!==null",
"data":{
".write":"root.child('project/'+$projectId+'/permissions/'+auth.uid).val()==='write'"
}
}
}
}
}
QUESTION:
How can I query all projects by UserId? where project.permissions.contains(userId) Or at least how to get all subkeys current user has permissions to read?
If I query just this:
let query = firebaseApp.database().ref("/project").orderByKey();
let val = await query.once("value");
it throws an error "Error: permission_denied at /project: Client doesn't have permission to access the desired data."