Home > Mobile >  Typecasting in Firebase Realtime database security rules
Typecasting in Firebase Realtime database security rules

Time:12-03

We have a database we store UIDs as numbers inside a nested structure.

I've been trying to setup conditional access to certain nodes, and wanted to know if there is any way to typecast inside rules.

Here's an example to explain the problem:

".read": "data.child('members/0').val() == auth.uid && auth.uid!=null",

In our DB, under members/0 we have user UIDs stored as numbers, whereas auth.uid seems to return a string.

This condition always fails due to this (At least what I think is happening).

What would be the best way to go about resolving this issue without having to tinker with the database itself?

EDIT

Problem: I'm trying to read a database value stored as a Number and compare it to auth.uid, which is a String in Firebase database security rules. Wanted to know if there is any way of comparing these two values

CodePudding user response:

UIDs in Firebase are by definition strings, so even if your underlying auth system uses numeric IDs they will be stored as strings in Firebase. I highly recommend storing the UIDs in Firebase as strings too for that reason.

That said, you can coerce the number to a string in your current rule and then compare it to the UID string:

".read": " '' data.child('members/0').val() == auth.uid && auth.uid!=null "
//                    
  • Related