首页 > 解决方案 > 用户如何在 vue.js 上使用 firestore 读取/写入自己的数据?

问题描述

我的目标是让用户只读取和写入自己提交的数据。

提交数据时,我可以获取它,但它目前可供所有用户查看。

在 Firestore 上使用新的安全规则:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /users/{userId} {
          allow read, update, delete: if request.auth.uid == userId;
          allow create: if request.auth.uid != null;
        }
      }
    }

Uncaught Error in onSnapshot: FirebaseError: Missing or insufficient permissions.即使我的集合被命名users并且我的文档 id 是用户 uid,我也可以在 chrome 上进入控制台。

在这里,我使用插值和 v-for 来显示数据: <sidebar-link id="user.uid" :to="/${user.title} " :name="${user.title}" icon="tim-icons icon-atom" v-for="(user) in users" :key="user.title"/>

在此处输入图像描述

在这里,我正在尝试从 firestore 获取数据:

      created () {
          db.collection('users').onSnapshot(res => {
            const changes = res.docChanges()
            changes.forEach(change => {
              if (change.type === 'added') {
                this.users.push({
                  ...change.doc.data(),
                  id: change.doc.id
                })
              }
            })
          })
        }

添加 if 语句read, update and delete显然可以让我再次看到数据,但它也允许所有用户看到它,这不是我想要的安全性。

标签: javascriptfirebasevue.jsgoogle-cloud-firestorefirebase-security

解决方案


推荐阅读