I'm building an AngularjS app that allows users (authenticated with Firebase anonymous) to submit their Fullname, Company and Email address to get on our distribution list.
What I want to accomplish is to disable the users form entering data twice, hence checking if the data submitted already exists in the database. When it does, it should deny the submitted data, keeping my DB clean. The UI will take care of all the error messages and so on.
My endpoint: http://someurl.firebaseio.com/accounts/
Sample data submitted to /accounts/ via push() :
{fullName: "John Doe", company: "Pet's Place", email: "john@petsplace.org"}
Firebase security rules setup:
Goal: Everybody may write to /accounts/ to register, only unique values and with validations for length and isString. Reading is not allowed for the anonymous users.
{
"rules": {
".read": "auth !== null",
".write": "auth !== null",
"accounts": {
".write": "auth !== null && newData.exists()",
"$accountID": {
".read": false,
".validate": "newData.hasChildren(['fullName', 'company', 'email'])",
"fullName": {
".validate": "newData.isString() && newData.val().length > 0 && newData.val().length < 50 && !newData.val().contains('admin')"
},
"company": {
".validate": "newData.isString() && newData.val().length > 0 && newData.val().length < 50"
},
"email": {
".validate": "newData.isString() && newData.val().length > 0 && newData.val().length < 50"
}
}
}
}
}
The result after (multiple) submit
{
"accounts" : {
"-JY249g70KL-XG0c6ekM" : {
"company" : "Pet's Place",
"email" : "john@petsplace.org",
"fullName" : "John Doe"
},
"-JY249y2IwFWAYwEqC4Q" : {
"company" : "Pet's Place",
"email" : "john@petsplace.org",
"fullName" : "John Doe"
},
"-JY24ACApcPH2_jiU5PD" : {
"company" : "Pet's Place",
"email" : "john@petsplace.org",
"fullName" : "John Doe"
},
"-JY24AL8QOKQRiTh3Oqm" : {
"company" : "Pet's Place",
"email" : "john@petsplace.org",
"fullName" : "John Doe"
}
}
}