Home > database >  How to write to the firebase realtime database that only allows a certain UID to edit?
How to write to the firebase realtime database that only allows a certain UID to edit?

Time:01-04

{
  "rules": {
        "gallery": {
            "$artwork": {
                    ".read": "true",
                ".write": "auth.uid === \"UID\""
          }
      },
      "gallerymap": {
        ".read": "true",
        ".write": "auth.uid === \"UID\""
      }
  }
}

I am creating a gallery that stores the artworks information in a real-time database, so I can add artwork through a page and store it. How do you send a write request along with the UID?

I can't find any documentation for sending a write request along with the UID.

CodePudding user response:

To set the auth variable (including auth.uid) you have to sign the user in to Firebase Authentication. Once a user is signed in to Firebase Authentication, their token is automatically passed with any calls to the Realtime Database SDK, and the auth. variable in your rules is automatically populated from that.

If you're not using Firebase Authentication yet, the simplest way to add it is by implementing anonymous authentication which doesn't require the user to enter any credentials. I usually use anonymous authentication as a first step, print the UID that I get back in my code, and then copy/paste that into my security rules.

  • Related