Charles Jr
Charles Jr

Reputation: 9109

Firebase read/write rules with Flutter not working?

I'm having trouble with permissions for my Firebase Security Rules. I'm logging the following errors..

[Firebase/Database][I-RDB03812] Listener at /users/4eb8920a-e407-4488-bce4-c6f64f7b0891/Following/4eb8920a-e407-4488-bce4-c6f64f7b0891 failed: permission_denied
[Firebase/Database][I-RDB03812] Listener at /UserVideo/4eb8920a-e407-4488-bce4-c6f64f7b0891 failed: permission_denied
[Firebase/Database][I-RDB03428] Using an unspecified index. Consider adding ".indexOn": "displayName" at /users to your security rules for better performance

Here is my rule structure:

{
  "rules": {
    "$user_id": {".write": true, ".read": true}

  }
}

Here is how I'm structuring my data...

 UserVideo
     080d4874-47d9-4f4e-b815-9b8dc9c8a2ba
     13566fd4-047b-4d62-b2e5-e885e6667430
     2bd7038f-5490-470e-94eb-87695e4b1071
     2d84a15b-d0aa-4671-9f59-02f9d2ac5207
     3af71559-5c51-40c6-b2aa-8631671c2c25
     3fFH6evUANf3WDbDS3caiQu9crD3
     4eb8920a-e407-4488-bce4-c6f64f7b0891
        Vid1
        Vid10
        Vid2
        Vid3

users
 4eb8920a-e407-4488-bce4-c6f64f7b0891
  FollowedBy
    followedBy: 
      2
 Following
    4eb8920a-e407-4488-bce4-c6f64f7b0891
 displayName: 
   "Charles"
 photo: 
   "placeholder"

My assumption was that my rule would let a user read and write to any available path, but I guess I missed something.

Upvotes: 0

Views: 1269

Answers (1)

Solo
Solo

Reputation: 58

I was running into this issue earlier. I looks like you are not using uid's but rather another id instance. The way to get the uid from the firebase instance is as follows:

final FirebaseAuth auth = FirebaseAuth.instance;

var tempUser = await auth.currentUser();
currentUser= tempUser.uid;

Make sure you are pushing to the uid child under the root.

reference.child(currentUser).push().set({                                        
  'video': 080d4874-47d9-4f4e-b815-9b8dc9c8a2ba,                                               
});

Finally, set your read and write rules in firebase like this.

{
  "rules": {
    "$user_id": {
      ".write": "auth != null && auth.uid == $user_id",
      ".read": "auth != null && auth.uid == $user_id"
    }
  },
}

Upvotes: 2

Related Questions