Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase storage rules - Unexpected 'userId'

I'm trying to set a rule, in firebase for storage, where only the authenticated user can write his avatar image called avatar

This is firebase docs example:

// Grants a user access to a node matching their user ID
service firebase.storage {
  match /b/{bucket}/o {
    // Files look like: "user/<UID>/path/to/file.txt"
    match /user/{userId}/{allPaths=**} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

and these are my rules defined in storage.rules file:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }

    match /images/{allPaths=**} {
      allow write: if request.resource.size < 5 * 1024 * 1024
                    && request.resource.contentType.matches("image/.*")
                    && request.resource.contentType == resource.contentType
    }

    match /images/user-{userId}/avatar.* {
      allow read;
      allow write: if request.auth.uid == userId;
    }
  }
}

When I deploy the error thrown is

[E] 13:25 - Unexpected 'userId'

I can't find anything else in the docs telling me how to defined this userId. The docs specify it and what am I doing wrong?

like image 372
João Cunha Avatar asked Jan 24 '18 18:01

João Cunha


1 Answers

Like both commenters said the syntax doesn't allow "folders" with -

So you must opt for a structure like

/images/user/{userId}

making the rule like:

match /images/user/{userId}/avatar.* {
  allow read;
  allow write: if request.auth.uid == userId;
}
like image 107
JuliaDias Avatar answered Sep 18 '22 13:09

JuliaDias