I'm trying to add User data into Cloud Firestore after using Firestore Auth when a new user is created.
$("#signup_btn").click(function () {
var firstname = document.getElementById("firstname").value;
var lastname = document.getElementById("lastname").value;
var email = document.getElementById("email_signup").value;
var password = document.getElementById("password_signup").value;
firebase.auth().createUserWithEmailAndPassword(email, password).then(cred=>{
this.createNewUserData(firstname,lastname,email);
}).catch(error =>{
var user = firebase.auth().currentUser;
user.delete();
var errorMessage = error.message;
window.alert("Error: "+ errorMessage);
});
});
function createNewUserData(firstname, lastname, email){
db.collection("users").add({
firstname: firstname,
lastname: lastname,
email: email
})
.then(function (docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function (error) {
console.error("Error adding document: ", error);
});
}
The above code I've implemented is able to add user data into the firebase Auth however it doesn't add any data into the firestore database. Is there a better way to implement this function?