Search code examples
firebasereact-nativefirebase-securityfirebase-storagereact-native-fetch-blob

What is the correct rule in firebase that allows any user to save an image and any authenticated user read others users avatars?


I am using react-native with react-native-fetch-blob and its pollyfills to upload an image to firebase storage. I could not follow the docs and after many tries I decided to ask how can set the proper firebase storage rule to avoid this message:

Firebase Storage: User does not have permission to access 'avatars/dTRYaMBELiNySUoml7h7JrNyZsg2

The dTRYaMBELiNySUoml7h7JrNyZsg2 is the uid, 'cause I organized my tree as gs://_______.appspot.com/avatars is the root of all my users avatars, so with any user UID I can retrieve its avatar.

What's is the correct firebase storage rule to allow a user upload its image and allow any authenticated users to read these avatars?


Solution

  • Something like:

    service firebase.storage {
      match /b/{bucket}/o {
        match /avatars/{userId} {
          allow read: if request.auth != null;
          allow write: if request.auth.uid == userId;
        }
      }
    }
    

    Per: https://firebase.google.com/docs/storage/security/user-security