1

I'm adding data to FireStore like this :

onPressed: () async {
  // signInAnonymously();
  UserCredential userCredential =
      await FirebaseAuth.instance.signInAnonymously();
  addReminderToFirebase();
  validateForm();
},

.

Future<DocumentReference> addReminderToFirebase() {
  return FirebaseFirestore.instance
      .collection('Reminders')
      .add(<String, dynamic>{
    'text': "Remember to do Laundry",
    'timestamp': DateTime.now(),
    'userId': FirebaseAuth.instance.currentUser!.uid,
  });
}

Currently I have documents everyone has access to it's all mixed up

How do I make each user have their own data ? Not accessible by anyone else ?

2 Answers 2

1

You should fetch the data of the specific user by using their userId.

You can fetch current user data by using the following snippet.

await FirebaseFirestore.instance
        .collection('Reminders')
        .where('userId', isEqualTo: FirebaseAuth.instance.currentUser!.uid);
Sign up to request clarification or add additional context in comments.

7 Comments

How would I implement it in my code ? I don't see where
I don't understand how I then add my data ^^? I'd like to upVote but I need 10 more points
You should add this code where you are displaying reminders. In this way, only the reminders of a currently logged user will be fetched and displayed.
ok thank you - how do I grab a value from this document ?
I'd like to upvote your answer but it doesn't allow me to
|
1

It seems you have a field userId which can be used to get creator of that reminder. You can check if the user requesting the reminder is the same person i.e. the UID of requester is same as the userId field. Try these security rules:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /Reminders/{reminderId} {
      allow read, write: if request.auth != null && resource.data.userId == request.auth.uid;
    }
  }
}

Then to fetch reminders of a single user you can use queries

var currentUser = FirebaseAuth.instance.currentUser
if (currentUser) {
 remindersCollection.where("userId", "==", currentUser.uid)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.