I'm trying to configure my firestore security rules so that all users can read data, but only logged in users can make posts and delete their own posts. The delete functionality isn't working and produces the following error:
FirebaseError: Missing or insufficient permissions.
I've configured my security rules as follows:
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;
}
..and the component controlling deletions is as follows:
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
I also tried allow delete: if request.auth.token.admin ==true;
, with no luck. Any suggestions?
userId
field of the document you're trying to delete. There should be enough information in the question so that we can reproduce the behavior. – Doug Stevenson