Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Redirección de autenticación de usuario de FireBase a otra página

Creé signupPage.html para autenticar a un usuario y registrar información en la base de datos en tiempo real en firebase usando este código:

 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); }); });

Luego, cuando hago clic en el botón Enviar, todo funciona y el usuario se autentica y la información se almacena en la base de datos en tiempo real. Ahora quiero redirigir al usuario a una página de inicio de sesión después de enviar su registro. En mi código en "aquí es donde se redirigirá la página", puse location.href = "login.html". Esto redirige la página y autentica al usuario, pero ya no almacena los datos en la base de datos en tiempo real. ¿Alguna sugerencia?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Estabas cerca. set() es una acción asíncrona, por lo que al agregar la redirección donde estaba, redirigiría antes de que set() tuviera la oportunidad de ejecutarse. Primero debe esperar a que termine set() y luego redirigir.

 signUp_button.addEventListener('click', (e) => { const email = document.getElementById('email').value; const password = document.getElementById('password').value; createUserWithEmailAndPassword(auth, email, password) .then(async (userCredential) => { //signed up const user = userCredential.user; // log to database & wait for it to finish! return set(ref(database, 'users/' + user.uid), { email : email }) }) .then(() => { alert('User Created'); // avoid alert, it blocks user input! // update a div with an information message instead location.href = "login.html"; }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; alert(errorMessage); // avoid alert, it blocks user input! // update a div with an error message instead }); });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!