Below is the code which allows the user to store data into Firestore. The problem is, the uploaded data is viewable for all the users logged into the app. How would I be ab
You would need to add a firestore rule to prevent a user from adding data to another user. Just check the auth userid in firestore and compare it to the userid on the document.
How to only store data for current logged in user using Firestore
You can achieve the same thing if you pass the uid
to the document()
method. So the following lines of code used in Firebase real-time database:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId = user.getUid();
mDatabaseRef = FirebaseDatabase.getInstance().getReference(FB_DATABASE_PATH).child(userId);
Is equivalent in Firestore with:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference uidRef = rootRef.collection("users").document(uid);
POJO pojo = new POJO();
uidRef.set(pojo);