I want to create user based security for Firebase/Storage. Below allow write works well if I do only upload images. But It prevents deleting a photo. How can I create proper security rule to this case?
service firebase.storage {
match /b/<bucket>/o {
match /{allPaths=**} {
allow read: if request.auth != null;
}
match /users/{uid}/{filename} {
allow write: if isCurrentUser(uid);
allow write: if isImage() &&
isCurrentUser(uid) &&
lessThanNMegabytes(n) &&
request.resource !=null &&
filename.size() < 50;
}
}
}
function isCurrentUser(uid) {
return request.auth.uid == uid;
}
function lessThanNMegabytes(n) {
return request.resource.size < n * 1024 * 1024;
}
function isImage() {
return request.resource.contentType.matches("image/.*");
}
I would use this to check if you are creating/updating a file or removing it
match /users/{uid}/{filename} {
allow write: if isCurrentUser(uid);
allow write: if resource == null ||
( isImage() &&
lessThanNMegabytes(n) &&
request.resource !=null &&
filename.size() < 50 );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With