How to only store data for current logged in user using Firestore

前端 未结 2 1969
情话喂你
情话喂你 2021-01-03 09:06

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

相关标签:
2条回答
  • 2021-01-03 09:39

    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.

    0 讨论(0)
  • 2021-01-03 09:45

    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);
    
    0 讨论(0)
提交回复
热议问题