1

I'm storing newsletter subscriber emails in the firebase database in this way:

enter image description here

I'm trying to write a firebase rule to allow the writing only if there's no the same email in the db.

this is the emails data structure:

emails: {
  id1: {
    email:"banana@gmail.com"
  },
  id2: {
    email:"watermelon@gmail.com"
  }
}

and this is my firebase rule for emails:

match /emails/{document=**} {
  allow read: if request.auth != null
  allow write: if request.resource.data.email != ???
}

How can i check if each id*.email is different from the query one?

I've already checked similar questions on StackOverflow but the answers are not very clear to me.

Thank you!

  • It's not possible to configure Firestore to enforce field uniqueness. It can only ensure document ID uniqueness within a collection. So, you'll have to build yourself, either using document IDs or something else. – Doug Stevenson Dec 09 '20 at 16:04
  • So i suppose that the right way is to store emails in this way `emails: { banana@gmail.com: { email:"banana@gmail.com" }}` by using the email as the document ID. but how is it possible to do so? – Lorenzo Case Del Rosario Dec 09 '20 at 16:28
  • 2
    If you have a new question, post it separately along with the code you have that doesn't work the way you expect. – Doug Stevenson Dec 09 '20 at 16:54

0 Answers0