Skip to content
Advertisement

Authorize read if uid matches write uid – Realtime Database

I have a functioning app where users can signup/login, upload images, and view images that have already been uploaded by themselves, they can’t see other’s images and vice versa. My rules are therefore UID of requesting user = uid of user who uploaded the image.

Here is the code:

JavaScript

Here are the corresponding realtime database rules.

JavaScript

It should be a fairly simple fix to check the user’s UID against the user who uploaded the image’s UID, however I have not been successful with my attempts. Any help is appreciated.

Advertisement

Answer

When you’re adding Model objects under the following reference:

JavaScript

Then indeed you need a query to get the objects that correspond to a particular user. If don’t want that, then you can restructure your database a little bit, so you can point exactly to the user images:

JavaScript

This means that you’ll be able to get the data really fast because when you don’t use a query, there is nothing that you need to compare against.

Now, to read that objects that correspond to a particular user, you only need to use the following reference:

JavaScript

Besides that, the rules you have added, only allow a user to read and write data under users/$uid, and will indeed reject any operation that you perform under images. So if you want to allow a user to read and write data only its own data, then you should also add another rule to allow that:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement