【问题标题】:Firebase security rules - delete rule not workingFirebase 安全规则 - 删除规则不起作用
【发布时间】:2020-10-24 23:16:58
【问题描述】:

我正在尝试配置我的 Firestore 安全规则,以便所有用户都可以读取数据,但只有登录的用户才能发帖和删除自己的帖子。删除功能不起作用并产生以下错误:

FirebaseError:权限缺失或不足。

我的安全规则配置如下:

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents{
  match/gig-listing/{document = **} {
  allow write: if request.auth.token.admin ==true;
  allow delete: if isAuthenticated() && request.auth.uid == resource.data.userId;
  allow read;
  }
  }
}

function isAuthenticated(){
return request.auth != null;
}

..控制删除的组件如下:

import React, {useState, useEffect} from 'react'
import Giglisting from './Giglisting'
import Button from "@material-ui/core/Button";
import { withStyles } from '@material-ui/core/styles';
import firebase from 'firebase'

const StyledButton = withStyles({
    root: {
      background: '#54ADA6',
      borderRadius: 3,
      border: 0,
      color: 'white',
      height: 30,
      padding: '0 30px',
      marginRight: '1px'
      
    },
    label: {
      textTransform: 'capitalize',
    },
  })(Button);


const UniqueVenueListing = (props) => {
    
const gigList = props.gigList
const ref = firebase.firestore().collection('gig-listing')

const deleteGig = (gigs) => {
    ref
    .doc(gigs.id)
    .delete()
    .catch(err => {
        console.error(err)
    })
}

    return(
        <div>
          {
              gigList.map(gigs => {
                  let name = gigs.data().name
                  let genre = gigs.data().genre
                  let time = gigs.data().time
                  let tickets = gigs.data().tickets
                  let price = gigs.data().price
                 return <Giglisting
                 gigtitle = {name}
                  genre = {genre}
                  time = {time}
                  buytickets = {tickets}
                  price = {price}
                  button = {<StyledButton onClick ={() => deleteGig(gigs)}>Delete Gig</StyledButton>}
                  />
              })
            }
        </div>
    )
}

export default UniqueVenueListing

我也试过allow delete: if request.auth.token.admin ==true;,但没有成功。有什么建议吗?

【问题讨论】:

  • 您的规则取决于现有文档的内容,我们看不到。请编辑问题以显示所有涉及的数据,包括您从代码中记录的 uid,以及您尝试删除的文档的 userId 字段。问题中应该有足够的信息,以便我们可以重现该行为。

标签: javascript firebase google-cloud-firestore firebase-security


【解决方案1】:

您可以尝试使用以下安全规则配置,以避免与您在共享的安全规则配置中定义的write 规则发生任何冲突。请注意,通过细化操作打破write 规则,您可以隔离delete 规则并获得所需的行为。查找所有相关信息here

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Matches any document in the 'gig-listing' collection or subcollections.
    match /gig-listing/{document=**} {
      // Allow everyone to read documents in the 'gig-listing' collection 
      //or subcollections
      allow read;
      //Separating the write functionality as per granular operations 
     //to isolate the delete command
      allow delete: if request.auth.uid == resource.data.userid;
      allow create, update: if request.auth.uid != null;
    }
  }
}

我发现this other section 的文档对于定义安全规则和如何查询数据非常有用。

【讨论】: