Search code examples
firebasefirebase-authenticationanonymous

Listen for changes if anonymous firebase account is upgraded from server side


How do I listen for anonymous account changes? I have noticed, that the onAuthStateChanged won't change if I "upgrade" the user from server side.

// server side "upgrade"
const authUser = await updateUser(uid, {
  email, emailVerified: true, password, displayName: `${firstName} ${lastName}`, disabled: false
});
(...)
return await auth.createCustomToken(uid), 

My checkout flow:

  1. User finished the checkout with a message to check his mails (gets an anonymous user id)
  2. Receives an email to create his account
  3. Most likely opens the email on mobile or in another tab / browser
  4. Creates the account with username/password and receives a token
  5. Old-Tab content won't change

My problem is that if the user confirms his account, it will most likely happen in a new tab or on another device and leaves the old tab in an outdated state. I'd like to update that tab as well with a welcome message.


Solution

  • There is no listener that applies to account changes in Firebase Auth. You will need another way to somehow signal to the app that something has changed. Two options are:

    1. Have the client listen to a document in Firestore or a location in Realtime Database to observe a change, and have your backend write to that location when an upgrade happens.
    2. Use FCM to ping the app.

    In either case, you will need to manually force a reload of the current user by calling reload() on the current User object.