I am relatively new to firebase and have been using it to integrate Firestore and authentication with a ReactJS app I'm working on.
I know that collections hold documents, and the documents in effect hold key value pairs. I have a collection of songs and within that I have the following fields:-
Created Timestamp
Lyrics
Song Title
I am able to login a user with firebase authentication get the UID of the user when they sign in and create a new song, the entry is made to the database with the document ID as the UID.
All this only works if I have my rules set to
match /{document=**} {allow read, write:}
When I change my rules to this:-
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /songs/{songID} {
allow read: if request.auth!=null;
allow write: if isValid();
}
function isValid(){
return request.resource.data.uid == request.auth.uid;
}}}
I get the following error when I run it.
Unhandled Rejection (FirebaseError): Missing or insufficient permissions.
I just want to ensure that the logged in user can read, write and update his/her own songs and nothing apart from that.
1) I'm unable to understand how to frame the firebase rules to achieve this and prevent permission errros.
2) If I use the document ID as the UID, i.e the same document ID for 5 songs. How would I be able to update the song title/lyrics in the songs since the document ID for a particular user would be the same?