首页 > 解决方案 > firestore 邮政限制

问题描述

我设法在应用程序中使用了firestore。用户可以发布一个博客,该博客将实时更新给所有用户。我想限制用户只能从他们的帐户发布一个博客。我怎样才能做到这一点?如何限制用户发布多个帖子?

标签: fluttergoogle-cloud-firestore

解决方案


您可以使用 Firestore 规则

service cloud.firestore {
    match /databases/{database}/documents { 
        match /blogs/{blogId} { 
            allow create: 
                if request.auth.uid == resource.data.userAuthId &&
                request.resource.data.authorNumOfBlogs == 0

            allow update, delete: 
                if request.auth.uid == resource.data.userAuthId
        }
     }
 }

如果请求与当前用户 id 匹配并且该用户只有一篇博客文章,这将只允许用户创建博客。

确保authorNumOfBlogs在您的/blogs集合中创建一个字段,并在用户创建/删除博客文章时相应地增加/减少它。此外,如果您尚未在 firestore 中跟踪用户的 uid,请在您的集合中添加一个userAuthId字段。/users


推荐阅读