Recently, my firebase project has been experiencing an issue where it is unable to create new documents after the last build. While it can save and update existing documents, the function for creating new ones seems to be failing.
The function responsible for creating new documents should generate a unique key based on a newly created UID as shown below:
const sendInformationHandler = () => {
const customerEmailCurrent = customerEmail.current.value
const customerPassword = "password"
if (checkUserEmailExists(customerEmailCurrent)) {
createUser(
{
email: customerEmailCurrent,
password: customerPassword
}
).then(function(user) {
firebase
.firestore()
.collection('sessions')
.doc(user.data.uid).set(
{...context.state}
).then(() => {
console.log("DATA SAVED SUCCESSFULLY!")
}).then(() => {
dispatch({ type: refreshContextFromCache, payload: INITIAL_CONTEXT})
}).then(() => {
push('/');
})
}).catch(err => console.log("AN ERROR OCCURRED", err))
} else {
console.log("EMAIL ALREADY EXISTS!!!!!!")
}
};
I have configured checkUserEmailExists to always return true. The createUser function is working fine to create new users in Firebase cloud functions. However, when this function is triggered, it redirects before encountering an error.
The error message indicates a permissions issue. It's strange because my Firebase API key remains unchanged, and I can still modify existing records without any problems.
My database rules are also intact and rely on tokens for authentication, which are still functioning correctly:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.token.admin == true;
}
match /{document=**} {
allow create: if request.auth.token.coach == true;
}
match /sessions/{userId} {
allow read, write: if request.auth.token.customer == true && request.auth.uid == userId;
}
}
}
Prior to deployment, I made sure to switch to the correct Firebase project using "firebase use" in the CLI and Google Cloud locally.
Here is a snippet of my firebaserc configuration:
{
"targets": {
"prod": {
"hosting": {
"prod": [
"prod-project"
]
}
},
"develop": {
"hosting": {
"prod": [
"prod-project"
]
}
},
"prod-project": {
"hosting": {
"prod": [
"prod-project"
]
}
},
"ammonite-testing": {
"hosting": {
"prod": [
"prod-project"
]
}
}
},
"projects": {
"prod": "prod-project",
"default": "prod-project"
}
}
Additionally, here is a snapshot of my firebase.json file:
{
"functions": {
"source": "functions"
},
"hosting": {
"target": "prod",
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
These configurations seem correct, so why am I facing issues with Firebase permissions now? Why does saving data work while creating new documents triggers errors, especially after the latest CI build?