I am trying to make a Trello-inspired app using React and Firestore. I'm now doing the feature where users on the board will invite or add others users.
The problem is when users are looking for other users, it is done on the frontend. My security rule allows reading of the users collection if the user:
allow read: if request.auth != null
The part where I query the users, trying to see if the email address or name matched
try {
const matchedUsers = []
const queryUsers = query(
collection(db, 'users'),
where('nameArray', 'array-contains', matchedUser)
)
const querySnapshot = await getDocs(queryUsers)
querySnapshot.forEach((doc) => {
const userObj = {
id: doc.data().userId,
name: doc.data().name,
photo: doc.data().photo,
}
matchedUsers.push(userObj)
})
setUsers(matchedUsers)
} catch (err) {
console.log(err)
}
As you can see there, it seems like I'm letting other users read other users' documents in the collection which will be problematic because my user doc contains email address too. I'm thinking that anyone can change the frontend code and get the email address of other users. Is my security rule for reading the users collection wrong? Because I know that if a security rule allows access or allow reads, then the user will have access to the entire document.
I want to know the best practices for doing this. I am new to the backend or database, I am just a newbie trying to create personal projects.
I found a suggestion from firestore Read only for specific fields but I have yet to try this
I'm thinking if I can do this, making the email a private subcollection since I only need the name or the userId. I really dunno if I'm doing the right thing or if my security rule request.auth != null
is fine.