2

I want to make a list of admins in firebase. Admins users should be able to add other admins, by email.

Ideally, I'd like a "list" like this:

"admins": {
  "a.b@gmail.com": true,
  "c.d@gmail.com": true
}

This would allow me to easily add new admins by email, and to use useful authentication rules (that verify users by email), like this :

".write": "root.child('admins').child(auth.email).val() === true"

But I can't use a list like that, because Firebase restricts special characters in keys.

How can I solve this?

My application is client-side javascript (so I can't use firebase-admin).

Thanks!

Hibuki
  • 544
  • 3
  • 14

1 Answers1

1

Every registered user has a firebaseID, so I would structure your data this way:

"users": { // Here you store user's data
    "firebaseID1": {
        "email": "a.b@gmail.com"
    },
    "firebaseID2": {
        "email": "c.d@gmail.com"
    }
},
"admins": {
  "firebaseID1": true,
  "firebaseID2": true
}

If you want to get the ID of a user given its email you can use firebase's queries:

firebase.database().ref("users").orderByChild("email").equalTo("a.b@gmail.com")

NB: firebase.com is the old website, for the updated documentation always look at firebase.google.com

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
  • 1
    Nice! How would you go about creating the "users" list? Every user writes himself when he logs in? Or can it be generated automatically somehow? @devid – Hibuki Aug 01 '17 at 02:06
  • 1
    You can let every user create his own entry at first login using security [rules](https://firebase.google.com/docs/database/security/) to avoid users setting wrong emails. You can also use [cloud functions](https://firebase.google.com/docs/functions/) with a firebase auth trigger :) – Devid Farinelli Aug 01 '17 at 06:45
  • 1
    Small typeo: "equalsTo" [should be](https://firebase.google.com/docs/reference/js/firebase.database.Query) "equalTo" @Devid – Hibuki Aug 11 '17 at 03:43