Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud firestore 仅当文档没有';不存在于Firebase Firestore中_Google Cloud Firestore - Fatal编程技术网

Google cloud firestore 仅当文档没有';不存在于Firebase Firestore中

Google cloud firestore 仅当文档没有';不存在于Firebase Firestore中,google-cloud-firestore,Google Cloud Firestore,我想要达到的目标很简单。我只需要在数据库中创建一个不存在的用户条目 应用程序流: this.db.doc('users/' + uid).set({username: Santa}) service cloud.firestore { match /databases/{database}/documents { match /users/{uid} { allow create: if request.auth != null &&

我想要达到的目标很简单。我只需要在数据库中创建一个不存在的用户条目

应用程序流:

this.db.doc('users/' + uid).set({username: Santa})
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{uid} {
      allow create: if 
        request.auth != null && 
        request.auth.uid == uid && 
        !exists(/databases/$(database)/documents/users/$(uid));
      allow update: if request.auth != null && request.auth.uid == uid; 
    }
  }
} 
  • 创建具有Firebase身份验证(获取UID)的用户
  • 使用UID作为键在数据库中创建用户文档
  • 客户端代码(创建操作):

    this.db.doc('users/' + uid).set({username: Santa})
    
    service cloud.firestore {
      match /databases/{database}/documents {
        match /users/{uid} {
          allow create: if 
            request.auth != null && 
            request.auth.uid == uid && 
            !exists(/databases/$(database)/documents/users/$(uid));
          allow update: if request.auth != null && request.auth.uid == uid; 
        }
      }
    } 
    
    firestore规则:

    this.db.doc('users/' + uid).set({username: Santa})
    
    service cloud.firestore {
      match /databases/{database}/documents {
        match /users/{uid} {
          allow create: if 
            request.auth != null && 
            request.auth.uid == uid && 
            !exists(/databases/$(database)/documents/users/$(uid));
          allow update: if request.auth != null && request.auth.uid == uid; 
        }
      }
    } 
    
    其他信息:

    this.db.doc('users/' + uid).set({username: Santa})
    
    service cloud.firestore {
      match /databases/{database}/documents {
        match /users/{uid} {
          allow create: if 
            request.auth != null && 
            request.auth.uid == uid && 
            !exists(/databases/$(database)/documents/users/$(uid));
          allow update: if request.auth != null && request.auth.uid == uid; 
        }
      }
    } 
    
    正如您可能已经知道的,代码不起作用。每次我运行客户端代码时,当前用户都会被一个新文档完全替换。但是,如果我从规则中删除以下行,一切都会按预期工作:

    allow update: if request.auth != null && request.auth.uid == uid;
    
    但现在问题来了,我如何保护用户文档中的数据不受未经授权的修改?有什么建议吗?

    this.db.doc('users/'+uid).set({username:Santa},{merge:true}) 它应该合并而不是替换。省略的字段将保持不变。
    在代码中使用逻辑,但不要在规则中使用逻辑。将addOnCompleteListener添加到用户集合,并在获得结果后执行一些操作

    getUsers.document(userUID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> doc) {
         if(!doc.getResult().exists()){
            //add new user
        }
    }                       
    

    如果仅在文档不存在时才允许创建文档,只需使用已有的
    allow create
    规则即可。因为您还有一个
    允许更新
    规则,所以也允许更新现有数据

    以下规则应足够:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /users/{uid} {
          allow create: if request.auth != null && request.auth.uid == uid;
        }
      }
    } 
    
    您不需要调用
    exists()
    ,因为
    allow create
    仅在数据不存在时适用

    现在谈谈你的问题:你应该澄清你的确切意思。现在,只有经过身份验证的用户才能修改自己的记录。如果您不想允许写入任意数据,请检查它

    以下是一些例子:

    我用过这个,它很管用。基本上,我确保用户通过
    request.auth!=null
    然后检查请求的资源是否为null。如果资源已经存在,则表示用户存在

    我添加了
    允许更新
    ,以防您只希望用户更改自己的数据

    match /users/{document} {
          allow create: if request.auth != null && resource == null;
          allow update: if request.auth != null && request.auth.uid == resource.data.author_uid;
    }
    

    我会考虑使用FixBasic函数来代替客户端修改用户集合。有一个“auth”触发器,允许您在创建新用户时执行stuf。提供的链接不再有效。请尝试以下链接:同意依赖代码,而不是逻辑的安全规则。