When creating a new document (in my example this is profile doc), security rules should check if profile with this email already exist. I create a additional function, that should search in "profiles" collection all profiles with specifil email and return is email already taken or not.
The documentation presents example (link):
service cloud.firestore {
match /databases/{database}/documents/some_collection: {
// Remember that, in Cloud Firestore, reads embedded in your rules are billed operations
write: if request.auth != null && get(/databases/(database)/documents/users/$(request.auth.uid)).data.admin) == true;
read: if request.auth != null;
}
}
But i can't use that, because i don't have s specifil profile id, i need to find all profiles in this collection with email.
My code:
match /profiles/{profileId} {
allow create: if allowToCreateProfileWithEmail(request.resource.data.email);
function allowToCreateProfileWithEmail (email) {
let profiles = firebase.firestore().collection("profiles").where("email", "==", email).get();
return profiles != null;
}
But this is not work, because firebase
doesn't exist. How can i fix my code to solve my problem?