See Array Changes in Firestore Security

筅森魡賤 提交于 2020-05-14 02:20:46

问题


I have a collection reviews where each review contains a list of uids of users who have liked it called likes. The schema looks like:

  • review (collection)
    • title string
    • author uid
    • likes [uid]
    • posted timestamp
  • user (collection) - uid
    • created timestamp
    • email string

Currently, I'm handling a user liking a review with:

    firebase.firestore().doc(rid).update({
      likes: firebase.firestore.FieldValue.arrayUnion(this.fetchCurrentUID())
    });

And unliking with:

    firebase.firestore().doc(rid).update({
      likes: firebase.firestore.FieldValue.arrayRemove(this.fetchCurrentUID())
    });

I only want to let a user add or remove their own uid from likes.

How can I write a security rule to ensure this? Specifically, I need to see how the list is being updated, for instance something like:

let newVals = request.resource.data.new_values // or something
return (newVals.length == 1 && newVals[0] == request.auth.uid)

来源:https://stackoverflow.com/questions/61528403/see-array-changes-in-firestore-security

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!