I'm developing a simple app that can upload photos. I'm using firebase firestore and storage. Every time I upload a new photo I create an object like this:
struct Item: Codable, Identifiable {
var id: UUID // photo name
var uidOwner: String // uid user owner
var emailOwner: String // email user owner
var path: String // photo path
var shared: [String] = []
}
I'm running into firestore rules to allow each user to view only the photos they uploaded.
For now the path on firestore is as follows:
For now I'm just using this rule which only checks that the user is logged in but in the firestorm playground it works and in Xcode I think the resource data uidOwner is null because I haven't access...
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /photos/{photo} {
allow read: if resource.data.uidOwner == request.auth.uid;
}
}
Could you tell me what rules I should use to allow each user to access only their own data? Thanks!!
Edit 1:
Here my request on iOS device:
func downloadImagesName(){
self.isLoading = true
self.itemsPhoto = []
// Reference
let imagesCollectionRef = db.collection("photos")
imagesCollectionRef.getDocuments(completion: { snapshot, error in
if let error = error {
print("Errore durante il recupero dei documenti: \(error.localizedDescription)")
self.isLoading = false
return
}
snapshot?.documents.forEach { doc in
do {
let item = try doc.data(as: Item.self)
self.itemsPhoto.append(item)
if doc == snapshot?.documents.last {
self.isLoading = false
}
} catch {
print("Errore durante la conversione dei dati: \(error.localizedDescription)")
self.isLoading = false
}
}
self.isLoading = false
})
}
Your query does not match your rules. Rules are not filters.
The query is saying that it wants all of the documents in the photos collection, but the rule says that is not allowed. The rule will not selectively exclude documents from a query. The rule requires that there is a "where" filter for equality on the query using the uidOwner
field as a filter, where the value must only the the user's UID. You have to add this filter to the query in order for it to work.