How to only allow one admin user to write Firebase Database?

11,422

This should work:

{
  "rules": {
    ".read": true,
    ".write": "auth.uid === 'dJrGShfgfd2'"
  }
}

Everyone in the world will be able to read the data, but only the user with UID dJrGShfgfd2 (and processes with administrative access) can write the data.

Share:
11,422
yigitserin
Author by

yigitserin

Updated on June 05, 2022

Comments

  • yigitserin
    yigitserin almost 2 years

    I am trying to set up a Firebase database that only I can write. So no user will have permission to write anything. But everyone will be able to read it.

    However, I could not set up the rules for it. I have one admin user that I created using Email/Password login, and I know its UID. Let's say my UID is: dJrGShfgfd2

    I tried these two methods, but they didn't allow me to write to database.

    {
      "rules": {
        "events": {
          ".read": true,
          ".write": "auth.uid === 'dJrGShfgfd2'"
        }
      }
    }
    

    .

    {
      "rules": {
        "users": {
          "$user_id": {
            ".read" : true,
            ".write": "$user_id === 'dJrGShfgfd2'"
          }
        }
      }
    }
    

    So how do I allow only one user with a specific UID to write anything to database?

  • Muhammad Umer
    Muhammad Umer about 7 years
    can you do this completely on client side only, meaning can you have paid members vs members system without having a backend. How would i go about that if user pays they become a member.
  • CraftedGaming
    CraftedGaming over 6 years
    what you can do is to check for a variable on the server that is saying that they have paid already. After which, you can add the rule on the permissions to check if it that variable is true meaning to say the user paid or false meaning to say the user did not pay
  • Tomer
    Tomer over 6 years
    How can I do this with an email instead of an uid?