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

291
Views
firebase.auth.onAuthStateChanged activado también en la actualización del token

He migrado recientemente a firebase JS SDK v9 (en modo de compatibilidad) y la primera vez que se actualiza el token de autenticación del usuario se llama al oyente auth.onAuthStateChanged .

¿Alguien ha experimentado el mismo problema?

Los documentos son claros After 4.0.0, the observer is only triggered on sign-in or sign-out.

El siguiente código reproduce el problema:

 <html> <script src="https://www.gstatic.com/firebasejs/9.1.3/firebase-app-compat.js"></script> <script src="https://www.gstatic.com/firebasejs/9.1.3/firebase-auth-compat.js"></script> <script> // Initialize Firebase const config = { apiKey: '', authDomain: '', databaseURL: '', projectId: '', storageBucket: '', messagingSenderId: '', appId: '', }; const email = "mail12345@mail.com"; const pass = "123456abc"; const app = firebase.initializeApp(config); const auth = app.auth(); const example = async () => { auth.onAuthStateChanged(async (user) => { console.log('onAuthStateChanged user uid', user ? user.uid : 'null'); if (!user) { try { await auth.createUserWithEmailAndPassword(email, pass); } catch (e) { console.log('user already created'); } console.log('signin') await auth.signInWithEmailAndPassword(email, pass); } }) setInterval(() => { console.log('force token renewal'); auth.currentUser.getIdToken(true); }, 5000) }; example(); </script> </html>

Si ejecuta por primera vez o si no hay sesión, obtiene el resultado:

 onAuthStateChanged user uid null <-- becuase there is no session stored user already created signin onAuthStateChanged user uid mqLHdtQhwJPaXqIEbDqFBwFoxLz1 <-- this is because the signin force token renewal <-- run each 5 secs

Tener una sesión almacenada si actualiza la página, encontrará el siguiente resultado:

 onAuthStateChanged user uid mqLHdtQhwJPaXqIEbDqFBwFoxLz1 <-- recover the session force token renewal onAuthStateChanged user uid mqLHdtQhwJPaXqIEbDqFBwFoxLz1 <-- run again after refresh token force token renewal <-- subsequent refresh token doesn not cause a call to onAuthStateChange

El problema está en la primera actualización del token. Después de depurar un poco en Chrome, descubrí que en la primera actualización del token, se invoca la función notificar notifyAuthListeners() desde el archivo auth_impl.ts , pero la variable this.lastNotifiedUid no está undefined , lo que hace que la actualización también se notifique a los oyentes onAuthStateChange .

ingrese la descripción de la imagen aquí

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

0

Finalmente, el problema se informó como un error en el proyecto firebase-js-sdk ( https://github.com/firebase/firebase-js-sdk/issues/5685 ) y se solucionó en un tiempo récord.

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!