I have developed a signupPage.html to validate a user and save information to the real-time database in Firebase with the following code:
signUp_button.addEventListener('click', (e) => {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
//signed up
const user = userCredential.user;
//log to database
set(ref(database, 'users/' + user.uid),{
email : email
})
//this is where page redirection
alert('User Created');
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});
});
Now, when I press my submit button, everything works fine. The user is authenticated, and their details are saved in the real-time database. However, I now want to automatically redirect the user to a login page after they submit their signup. In my code under "this is where page redirection", I placed location.href = "login.html". This change successfully redirects the page and authenticates the user, but it no longer saves the data into the real-time database. Any suggestions on how to address this?