简体   繁体   English

如何通过特定用户获取 firestore 中的数据

[英]how to get data in firestore by specific user

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)
    }
  };

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 forCloud Firestore and Realtime Datase depending on what you're using. Cloud FirestoreRealtime Datase的安全规则说明,具体取决于您使用的是什么。

How to get data in firestore by specific user?如何通过特定用户获取 firestore 中的数据?

You're looking for query operators with where , like this:您正在寻找带有where查询运算符,如下所示:

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

This will query only for documents that fulfill the where condition.这将仅查询满足where条件的文档。 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 withSecurity Rules .使用安全规则锁定集合。 For example, to make a collection named etkinlik only readable for clients with a matching uid :例如,要使名为etkinlik的集合仅对具有匹配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).假设etkinlik中的文档有一个字段uid (如您的源代码示例中所示;但您在那里也有owner ,所以这取决于您如何使用这些字段)。

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.最好使用 Firebase 控制台并测试 Playground 部分中的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM