0

I am using Firebase Web SDK in my project. At the moment I am creating a simple mailing list sign up form. I am trying to check that no duplicate emails get signed up. At the moment, it is possible to do this as firebase creates a message id one level up from the data being stored and I do not know how to validate it.

Here is my code:

db.ref("/signup_emails").push({
     name: d.name,
     email: d.email
}

Whenever this creates an entry into the database it does so with a message_id. Like so"

 signup_emails { 
     - M1itOVYTq-ySh_49rH3 {
          name: "John"
          email: "example@example.com"
     }
 }

How do I validate that the email has only been signed up once?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
JamesG
  • 1,552
  • 8
  • 39
  • 86
  • There is no way to check for duplicate **values** in the Firebase Database. If you want something to be unique, you should use that as they key of the nodes under a common root. For more on this, see https://stackoverflow.com/questions/39149216/firebase-security-rules-to-check-unique-value-of-a-child-askfirebase/39151205#39151205, https://stackoverflow.com/questions/35243492/firebase-android-make-username-unique, and many of these [search results](https://stackoverflow.com/search?q=%5Bfirebase-realtime-database%5D+unique+value). – Frank van Puffelen Mar 09 '20 at 00:57

1 Answers1

0

As you have things structured now, it is not possible with Firebase security rules. If you want to know if there's child node with a specific value, and you don't know its full path, you would have to perform a query to find it. However, security rules don't have the ability to perform a query. You can only reference a node by its fully specified path.

If you want to use security rules to find a piece of data, that data will have to be the name of a node that contains that data. So, the email address, or some form of it, will have to be in the path, not in a value.

Or, you can push the logic into backend code that can perform the check for you.

Community
  • 1
  • 1
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441