Firebase Rules to let users see their own data, ER

2019-08-25 02:34发布

问题:

I am trying to figure out Firebase Rules that will allow my users to only see their data in their App.

Currently this is my Realtime Database File:

and this is my Firebase Set Rules:

 {
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth.uid === $uid"
      }
    },
    "jobs": {
      "$uid": {
        ".read": "auth.uid === $uid"
      }
    }
  }
}

How do i change the rules to be able to let users see their data that is relevant to their userid?

回答1:

It looks like you're using the user's UID as the key for their data in both jobs and users. In that case, you can ensure that each user can only read their own job and profile with:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth.uid === $uid"
      }
    },
    "jobs": {
      "$uid": {
        ".read": "auth.uid === $uid"
      }
    }
  }
}

Note that this is almost a literal copy of the sample in the Firebase documentation on securing user data.