Firestore安全性

编程入门 行业动态 更新时间:2024-10-24 20:21:11
本文介绍了Firestore安全性-仅允许已知字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不知道如何在Firestore中正确设置".validate"规则. 基本上,我希望允许用户文档仅包含我知道的字段: :

I can’t figure out how to properly set the ‘.validate’ rule in Firestore. Basically, I want to allow a User document to contain only the fields I know:

user { name: "John" phone: "2342222" address: "5th Avenue" }

除了上面的3个字段(姓名,电话,地址)之外,我不希望其他任何字段.

I dont want any other fields besides the 3 above (name, phone, address).

不会同时保存字段. 姓名和电话将首先保存,而地址仅在用户要编辑其个人资料时保存.

The fields WON’T be saved at the same time. name and phone will be saved first, and address will be saved only when user wants to edit his profile.

我尝试了以下规则,但似乎不起作用:

I've tried the rules below but don’t seem to work:

allow read: if request.auth.uid == uid; allow write: if request.auth.uid == uid && request.resource.data.keys() in ["name", "phone", "address"]

感谢您的帮助.

推荐答案

您可以将规则分开,以包含不同的create和update(以及delete)逻辑:

You can separate your rules to include different create and update (as well as delete) logic:

// allows for creation with name and phone fields allow create: if request.resource.data.size() == 2 && request.resource.data.hasAll(['name', 'phone']) && request.resource.data.name is string && request.resource.data.phone is string; // allows a single update adding the address field // OR (||) in additional constraints allow update: if request.resource.data.size() == resource.data.size() + 1 && !('address' in resource.data) && request.resource.data.address is string;

更多推荐

Firestore安全性

本文发布于:2023-05-25 10:41:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226450.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:安全性   Firestore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!