4

I have been making mobile application for iOS and Android platforms and want to prevent this duplicate entries which the apps make. I don't want to this on app side as I'll have to do it on both the platforms. Then I heard about the rules in the Firebase Database section and I tried couple of them but didn't work out for me. Please help me with the firebase rules.

enter image description here

Anirudha Mahale
  • 2,526
  • 3
  • 37
  • 57
  • I think you work with `Realtime database` ? is it ? not `Cloud Firestore` I saw some example at `Cloud Firestore` has this secure rules.. But not sure. – Cappittall Jan 20 '18 at 14:46
  • There is no way in security rules to make **values** unique, since it's an operation that inherently doesn't scale very well. Instead you'll want use the "thing that must be unique" as the key in a list in your database. Keys in a list are by definition unique. For example: if you use the `name` as the key for `/Dealers`, there can be only one `Anirudh`. This has been discussed many times before, so I recommend you try some of [these](https://stackoverflow.com/search?q=%5Bfirebase-security%5D+unique). – Frank van Puffelen Jan 20 '18 at 16:01
  • @HakanC Yes, I am using Realtime database. – Anirudha Mahale Jan 20 '18 at 16:57
  • @FrankvanPuffelen I'll go through this and see, Thanks! – Anirudha Mahale Jan 20 '18 at 16:58

1 Answers1

0

If you use Cloud Firestore, you may prevent double record something like this rule, but honestly I did not test it. I wrote it just from Firebase documents :

service cloud.firestore {
  match /databases/{database}/documents {

    // Checks name is not the same as resource and request data
    match /Dealers/{dealer} {
      allow write: if request.resource.data.name != resource.data.name;
    }
  }
}

for more information, check this

Cappittall
  • 3,300
  • 3
  • 15
  • 23
  • The rule you give simply ensures that you can't write data which already exists. This is indeed the most common approach. The approach for the Firebase Realtime Database would be exactly the same: if you want something to be unique, make it a key in the JSON. Those are by definition unique. – Frank van Puffelen Jan 20 '18 at 15:57