MarcS82
MarcS82

Reputation: 2517

How to check if email is verified in firebase on client side?

I want to check if a user has verified his email address while he's logging in. It seems I have to do this after the login. Any better way to do this?

this.authService
  .login(this.email, this.password)
  .then(res => {
    if(!res.user.emailVerified}) {
         // Log out again!
    } else {
        // redirect to next page
    } 
  });   

Upvotes: 0

Views: 133

Answers (2)

RameshD
RameshD

Reputation: 962

You would want to get user as from 'onAuthStateChanged' and check emailVerified paramter to perform you logic.

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    if(user.emailVerified){
         // proceed with your logic 
    }
  } else {
    // No user is signed in.
  }
});

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Your current code means that the check only runs when the user explicitly checks in. If that is what you want, this code implement it.


In many apps however the user only explicitly signs in once/rarely, and the app then restores their authentication state whenever it restarts. Firebase Authentication in fact does this behind the scenes: persisting the authentication state and then restoring it when the app is restarted.

To pick up this flow, you'll want to use an authentication state listener as shown in the documentation on getting the currently signed in user.

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

If you are only signing the user out to pick up when the emailVerified property has been updated, I would not do this, as that is just an annoyance to them requiring them to enter their password repeatedly. Instead you can also force their profile to refresh by calling user.reload() in your code.

Upvotes: 1

Related Questions