0

is there a possibility to check whether the data users are adding is already in the DB or is it possible only with doing it "manually" via Firebase cloud functions?

Let me give you a very simple example of what I mean:

I want to store users, however I need Security rules to look for duplicate of the email address and only if there is no matching email adress the received data can be stored.

DB structure:

root
 └── users
     └── uID
         ├── email
         ├── name
         └── gender

sent JSON:

{
   email: test@test.com,
   name: John Example,
   gender: male
}

Notice: I am not using those data for logging in. I am just storing data returned from the Facebook Graph API.

Thank you!

Community
  • 1
  • 1
TomasB
  • 604
  • 5
  • 18
  • For which platform, Android, iOs or Web? – Alex Mamo Jun 28 '17 at 12:17
  • 1
    @AlexMamo Security rules are set on the web. So It shouldn't be platform specific. – TomasB Jun 28 '17 at 12:37
  • Would be any help for you, to do it in code, with Android? – Alex Mamo Jun 28 '17 at 12:40
  • @AlexMamo I am working with the JavaScript SDK so I cannot do it on a client side since they could change the code a bit and see other users. – TomasB Jun 28 '17 at 12:43
  • You'll need to store a list of the email addresses that are already in use in the database. Then you can check against that list. See https://stackoverflow.com/questions/44802162/firebase-db-check-if-data-is-already-stored – Frank van Puffelen Jun 28 '17 at 14:28
  • @FrankvanPuffelen the link you post is pointing to this question. I've hoped there'll be solution without data redundancy. But I'll unfortunately have to do it your way. – TomasB Jun 28 '17 at 14:49
  • Data redundancy is normal in NoSQL databases. I often consider them the self-coded variant of automatically generated indexes in relational DBMSs. Sorry for the faulty link, it was meant to be: https://stackoverflow.com/questions/35243492/firebase-android-make-username-unique – Frank van Puffelen Jun 28 '17 at 14:51
  • @FrankvanPuffelen Does it have to be end-node or is it possible to se uID as the email? – TomasB Jun 28 '17 at 15:11
  • Given that you say you're not using Firebase Authentication, I don't understand what uID is in this context. But for ensuring uniqueness you must have a collection where the unique value is used as the key. – Frank van Puffelen Jun 28 '17 at 16:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147858/discussion-between-tomasb-and-frank-van-puffelen). – TomasB Jun 28 '17 at 17:23
  • 1
    The problem with storing the email in a key is not caused by the `@`, but by the `.`, which is not allowed by Firebase. Most developers simply convert the `.` to `,`, which is conveniently not allowed in email addresses. Also see my answer here: https://stackoverflow.com/questions/39149216/firebase-security-rules-to-check-unique-value-of-a-child-askfirebase – Frank van Puffelen Jun 29 '17 at 03:13

1 Answers1

0
"rules" : {
    "test" : {
       ".write" : "true && newData.val() !== data.child('email').val()",
       ".read" : true
 }
}

Hope this helps..!

Prags
  • 811
  • 5
  • 17
  • Thank you for your input however I wrote at the bottom of the question that I'm not using the data for Logging in therefore I cannot use the auth expression. – TomasB Jun 28 '17 at 12:48
  • if not use auth , then you remove the auth and try, it will not affect @TomasB – Prags Jun 28 '17 at 12:50
  • Well I had to chenge it to newData.child('email').val() to make it publishable however it still doesn't work (at least in the simulator) path: /users/test23 { "email": "123" } I have one user with email of "123" and it still lets you write. – TomasB Jun 28 '17 at 13:02
  • will you be able to post screenshot – Prags Jun 28 '17 at 13:07