exports.createUserProfileManual = functions.https.onCall(async (
data, context) => {
// Validate the data
const {uid, email} = data;
if (!uid || !email) {
throw new functions.https.HttpsError("invalid-argument",
"Missing UID or email.");
}
const userProfile = {
uid,
email,
createdAt: admin.firestore.FieldValue.serverTimestamp(),
};
try {
await firestore.collection("users").doc(uid).set(userProfile);
console.log(`User profile created for UID: ${uid}`);
return {message: "User profile created successfully"};
} catch (error) {
console.error("Error creating user profile:", error);
throw new functions.https.HttpsError("internal", error.message);
}
});
This is my firebase function, and where it is used:
`document.getElementById("register-form").addEventListener("submit", async (e) => {
e.preventDefault();
const email = document.getElementById("register-email").value.trim();
const password = document.getElementById("register-password").value.trim();
if (!email || !password) {
alert("Please provide both email and password.");
return;
}
try {
// Create user in Firebase Authentication
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
const user = userCredential.user;
console.log("User successfully created:", user.uid);
// Call the callable cloud function to create the user profile
const createUserProfile = httpsCallable(functions, "createUserProfileManual");
const result = await createUserProfile({ uid: user.uid, email: user.email });
console.log("Cloud Function result:", result.data);
alert("User registration and profile creation successful!");
} catch (error) {
console.error("Registration Error:", error);
alert(`Error: ${error.message}`);
}
});`
Online I got this https error: Error: Missing UID or email.
which seems to indicate that parameters aren’t passed properly to the function.
What is weird is that in my functions log in the console, I can see the data transmitted being correct.
I first tried to put the auth and creation both in the functions which didn’t work, the function I tried also to put the parameters as not mandatory and I correctly created a document in my firestore but of course without email.