Home > Software engineering >  how to get data in firestore by specific user
how to get data in firestore by specific user

Time:03-30

The logged in user can also see the activities of other users. but I only want the logged in user's activities to be seen. Do I need to make changes in the query section?

  useEffect(()=>{
    const q = query(collection(db,'etkinlik'));
    const unsub = onSnapshot(q,(snap)=>{
      const array = snap.docs.map(doc=>{
        return{
          id : doc.id,
          title : doc.get('title'),
          start: doc.get('start').toDate(),
          allDay : doc.get('allDay')

        }
      });
      setData([...array]);
    })
    return ()=>{unsub()}
  })  
 const handleDateClick = (args) => {
    if(args.jsEvent.altKey) {
      const title= prompt('Enter Title',args.dateStr);
      const event = {
        title: title ? title : args.dateStr,
        start: args.date,
        allDay: true,
        uid: uid,
        owner: user.uid
      }
      addDoc(collection(db,'etkinlik'),event)
    }
  };

CodePudding user response:

To restrict a user from seeing certain data (such as other user's data), you will need to develop database rules. Restricting one user from seeing data can also be done locally, but for security reasons you should develop this from the backend.

Security rule instructions for Cloud Firestore and Realtime Datase depending on what you're using.

CodePudding user response:

How to get data in firestore by specific user?

You're looking for query operators with where, like this:

query(collection(db,'etkinlik'), where('uid', '==', uid));

This will query only for documents that fulfill the where condition. However, this doesn't stop other users from query the data.

I only want the logged in user's activities to be seen

Lock the collection down with Security Rules. For example, to make a collection named etkinlik only readable for clients with a matching uid:

service cloud.firestore {
  match /databases/{database}/documents {
    match /etkinlik {
      allow read: if request.auth.uid == resource.data.uid;
    }
  }
}

Assuming that documents in etkinlik have a field uid (as indicated in your source code example; but you also have owner there, so it depends on you how you use those fields).

See docs, to deploy the Security Rules. It's a good idea to use the Firebase Console and test the rules in the Playground section.

  • Related