Do you store user_id property for a document as Reference or String?
item {
name: "Test"
user_id: "2i5uqlufac0QvPw1YRiE"
user_ref: "/users/2i5uqlufac0QvPw1YRiE"
}If you use the property as part of security rules (e.g. only item owner can read the document), you need to use String.
service cloud.firestore {
match /databases/{database}/documents {
match /items/{item_id} {
allow read: if request.auth.uid == resource.data.user_id;
// the following doesn't work
// allow read: if request.auth.uid == resource.data.user_ref.id;
// allow read: if request.auth.uid == get(resource.data.user_ref).id;
}
}
}NOTE: Based on get and Resource, get(resource.data.user_ref).id should work (but it doesn't, a bug maybe?)
Using reference provides context, as in you know the collection path is /users/*, which comes with pros and cons.
user_refwill returnDocumentReference, else you need to constructfirestore.collection("users").document(userId).- I suspect you need to update all
user_refif you ever change collection name or path. user_refcan be used to refer to different collections (e.g./usersor/adminsorprivate_users), just in case all users don't necessary resides in/userscollection.