I want to use the location variable in child in the description of the rule in firebase.

Asked 1 years ago, Updated 1 years ago, 50 views

Thank you for your continuous support.
I am currently creating chat software on firebase, but the back-end process did not work out as I expected.

I added read and write permissions to the chat room on the database, but I didn't know how to refer to the rule, so I asked you a question.

rule

{
   
   "rules": {
     "rooms": {
       ".read": "auth!=null",
         ".write": "auth!=null",
         "$room_id":{
                    ".read": "root.child('rooms') .child($room_id).child('read').val() == 'true',
                    ".write": "root.child('rooms') .child($room_id).child('write').val() == 'true',
         }
     }
   }
 }

Database

{
          
      "rooms": {
        "-ROOM_ID_1": {
          "admin"—false,
          "img_upload": false,
          "messages": {
            "-MB-NatBV1ybNEUM-DVS": {
              "name": "hoge."",
              "text": "Test"
            }
          },
          "read"—true,
          "room_name": "Room 1",
          "write"—true
        },
        "-ROOM_ID_2": {
          "admin"—false,
          "img_upload": false,
          "messages": {
            "-MB-BcDIyABa7Ngw23IM": {
              "name": "hoge",
              "text": "Test"
            },
            "-MB-NPjk4xd0h_JZTUjG": {
              "name": "hoge",
              "text": "Test"
            }
          },
          "read"—true,
          "room_name": "Room 2",
          "write"—true
     }
    }
 }

I'd like to see the write in the database room from the rule, but I'm having a hard time because I can't.

firebase

2022-09-29 22:06

1 Answers

I solved myself.
It looks like this because it's improved a little.

Database

{
  "admin-users": {
    "9k3Uf ------------------------"—true
  },
  "black-list": {
    "MAtSa -------------------"—true
  },
  "rooms": {
    "-MB3l ---------------": {
      "admin"—false,
      "author":",
      "img_upload": false,
      "messages": {
        "-MB3nu --------------": {
          "name": "Haru Haru",
          "photoUrl":",
          "text": "Test"
        }
      },
      "photoUrl":",
      "read"—true,
      "room_name": "Room 1",
      "write"—true
    },
    "-MB3m -----------------": {
      "admin"—false,
      "author":",
      "img_upload": false,
      "photoUrl":",
      "read"—true,
      "room_name": "Room 2",
      "write"—false
    }
  }
}

rule

{  
  "rules": {
    "admin-users": {
      ".read": "auth!=null&&!root.child('black-list').child(auth.uid).exists(),
      ".write": "root.child('admin-users').child(auth.uid).exists(),
    },
    "black-list": {
      ".read": "auth!=null&&!root.child('black-list').child(auth.uid).exists(),
      ".write": "root.child('admin-users').child(auth.uid).exists(),
    },
    "rooms": {
      ".read": "root.child('admin-users') .child(auth.uid).exists()||auth!=null&&!root.child('black-list') .child(auth.uid).exists(),
        "$room_id":{
                    ".read": "root.child('admin-users').child(auth.uid).exists()||auth!=null&&!root.child('black-list').child(auth.uid).exists()&data.child('read') .value(true"
              ".write": "root.child('admin-users').child(auth.uid).exists()||auth!=null&&!root.child('black-list').child(auth.uid).exists()&data.child('write').value"
        }
    }
  }
}


2022-09-29 22:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.