Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 397
yigitserin Avatar asked Jan 20 '17 20:01

yigitserin


People also ask

Are users in Firebase unique?

Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).

Can anyone access my Firebase database?

When you create a database or storage instance in the Firebase console, you choose whether your Firebase Security Rules restrict access to your data (Locked mode) or allow anyone access (Test mode). In Cloud Firestore and Realtime Database, the default rules for Locked mode deny access to all users.

What are the different roles that can be assigned to a user in Firebase?

In the Firebase console, you can assign any of the basic roles (Owner, Editor, Viewer), the Firebase Admin/Viewer roles, or any of the Firebase predefined product-category roles.


1 Answers

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.

like image 109
Frank van Puffelen Avatar answered Oct 08 '22 03:10

Frank van Puffelen