firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.85k stars 893 forks source link

Successful email verification should trigger the onAuthStateChanged function #3817

Open ethoman opened 4 years ago

ethoman commented 4 years ago

It would make so many people's lives easier, please consider making this change. Right now people have to make their own off brand listeners or add timers to check every couple seconds for updates - this would be such a quality of life improvement.

google-oss-bot commented 4 years ago

I found a few problems with this issue:

malcolmdeck commented 4 years ago

Hey ethoman,

I've filed b/169070231 to track this feature request internally.

This would actually be really difficult to implement on our side in a way that doesn't waste a significant amount of resources (e.g. bandwidth) or requiring a new pathway in our system to allow clients to listen for these types of changes.

You might instead consider using Email Link Sign In, which results in users only ever having verified emails as soon as they're signed in. I'm guessing that that would solve your problem

Hope that helps! ~Malcolm

ethoman commented 4 years ago

I appreciate the answer! I actually already use Email Link sign in. The issue is more that after email link sign in is triggered (when the user goes to their email and successfully verifies using the link sent to them), the emailVerified state in authuser is not updated. This means that the original tab the user was in when they sign up does not have updated information, though the new tab that is opened from the email does.

AnthonyLabaere commented 3 years ago

Hello, have you found a solution to this issue ?

Xstyler85 commented 3 years ago

I have the same problem

laurentpayot commented 3 years ago

I opened #2529 for the same reason. After a successful email verification, even updateCurrentUser() call does not trigger onAuthStateChanged. I had to use onIdTokenChanged instead of onAuthStateChanged, and to use firebase.auth().currentUser.getIdToken(true) instead of updateCurrentUser() to "manually" trigger the event.

smac89 commented 2 years ago

I came here to report the issue as well, but it seems even the firebase team do not have a ready solution. The solution that seems to work for us is to use setInterval (taken from this stackoverflow answer), and it seems to be doing a good job of informing us when the user's email has been verified.


Edit: instead of setInterval, we opted for setIntervalAsync from https://www.npmjs.com/package/set-interval-async. This means the reload can be awaited, so that we do not simply spam every second

jinyang1994 commented 1 year ago

+1

antoni0dev commented 1 year ago

Solution in the EmailVerificationPage where the user is sitting after being redirected from the RegisterPage:

useEffect(() => {
    const unsubscribe = firebaseAuth.onIdTokenChanged(user => {
      if (user) {
        (async () => {
          // needed because Firebase caches the user's info, and the emailVerified flag might not be updated in real-time after the user clicks the verification link in their email.
          await user.reload();

          if (user.emailVerified) {
            navigate(PATHS.accountSetup);
          }
        })();
      }
    });

    return () => {
      unsubscribe();
    };
  }, []);