I'm building a web application with Firebase and Angular5. I want to make possible to create/join private rooms (protected by password).
Currently I am at the stage of designing the database and can't really see how I can check user entering the correct room password without actually retrieving it from the firebase database (thus making it completely insecure).
Should I employ cloud functions for that matter. Or can it be done directly with firebase and I'm missing something?
You can do this in firebase.
Suppose part of your DB is laid out like :
/chatRooms/$roomID/password = $PASSWORD
Read/write permission to
/chatRooms/$roomID/password
is granted to the chatroom owner.To become a member of a chatroom, you must add a document to:
chatRooms/$roomId/users/$userId
with the following validation:The entry's userId must be the current user, and the password field must equal the password. A verification rule can access ANY data in the database, even if the user cannot access the data.
Below is an (untested) example that will show the basic principles.
In short:
code:
One way to do this that doesn't require a server is by embedding the password into the path of the room. For example: if you have a room "general" and password "correcthorsebatterystaple", then you could model this as:
And secure it with:
With these rules anyone can read the list of room names. But you can only read the messages of a room if you also know the password of the name.