Here's the use case:
Anyone can register to use the app. Authentication is handled by Firebase Auth.
Registered users can only read and write their own details. Access to the database is managed by Firebase security rules. For example:
{ "rules": { ".read" : "root.child('Admins').hasChild(auth.uid)", ".write" : "root.child('Admins').hasChild(auth.uid)", "Companies": { "$company" : { ".read" : "data.child('firebaseuid').val() === auth.uid", ".write" : "data.child('firebaseuid').val() === auth.uid", "firebaseuid" : { ".write" : ???? , ".read" : ????, }, "details" : { ".read" : "data.parent().child('firebaseuid').val() === auth.uid", ".write": "data.parent().child('firebaseuid').val() === auth.uid", }, }, }, }
QUESTION: What are the appropriate security rules for "firebaseui"?
Obviously, setting the read/write attributes true won't do. That's an open database.
"firebaseuid" : { ".write" : true , ".read" : true, },
Equally, :"auth !== null" doesn't work, because it would allow ANY registered user to read/write everybody's information.
"firebaseuid" : { ".write" :"auth !== null" , ".read" :"auth !== null", },
Setting the rule to allow only the registered user to read/write works...almost.
"firebaseuid" : { ".read" : "data.parent().child('firebaseuid').val() === auth.uid", ".write": "data.parent().child('firebaseuid').val() === auth.uid",
}
Almost, because this suffers from the First Write Problem. That is, the user would not be able to initialize the firebase uid ("firebaseuid") at registration, because in that first instance, the firebaseuid field is null, causing the security rule to return false.
One could add a firebase validator to the security rule, so that the user would be to write to "firebaseuid" when and only when the field is blank. This works well enough, if there is only one registered user. But suppose there are multiple admins per organization? In this case, this solution fails.
One might want to write a Firebase Cloud function that is triggered upon registration, but the Firebase user class doesn't contain enough information. At registration the only info available is email an password.
One could write a more complicated Cloud function triggered by a database create or http put, but every approach i can think of, has a security hole.
I realize this must be a very common problem, so there must be a simple answer. Can somebody please point me in the right direction?
"firebaseuid"
property meant to have the UID of the user that created that company as its value? – Frank van Puffelen