0

Trying to create an user and saving the data to Firestore while being logged with another User Id,but this makes the newly created signin automatically

firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
       // Get form elements
var managementForm = document.getElementById("management-form");
var managementName = document.getElementById("management-name");
var managementEmail = document.getElementById("management-email");
var managementPhone = document.getElementById("management-phone");
var managementrole = document.getElementById("management-role");
var managementPassword = document.getElementById("management-password");


// Reference to the "managements" collection
var managementsRef = firebase.firestore().collection("management");

// Add event listener for form submission
managementForm.addEventListener("submit", function (event) {
    event.preventDefault();

    // Get form values
    var name = managementName.value;
    var email = managementEmail.value;
    var phone = managementPhone.value;
    var password = managementPassword.value;
    var role = managementrole.value
    createManagementAccount(name, email, password, phone, role)
    async function createManagementAccount(name, email, password, phone, role) {
        try {
          const response = await firebase.auth().createUserWithEmailAndPassword(email, password);
          const newUserId = response.user.uid;
          await firebase.firestore().collection("management").doc(newUserId).set({
            name: name,
            email: email,
            password: password,
            phone: phone,
            role: role,
          });
          alert("Management account created successfully.");
        } catch (error) {
          console.error("Error creating management account: ", error);
        }
      }
});
    } else {
        // User is signed out, redirect them to the login page
        window.location.href = "login.html";
    }
});
UmairFarooq
  • 1,269
  • 1
  • 3
  • 8

0 Answers0