Skip to content
Advertisement

Why Firebase-Email-Verification doesn’t work?

$("#btn1").on('click', function(e) {
e.preventDefault(); // <== disable form submit

const email = signupForm['signup-email'].value;
const password = signupForm['signup-password'].value;


// sign up the user & add firestore data
const auth = firebase.auth();

const promise = auth.createUserWithEmailAndPassword(email, password);
promise.then(user => {
  user = firebase.auth().currentUser;
  user.sendEmailVerification();
}).catch(error => console.log);

});


$("#btn2").on('click', function(e) {
  e.preventDefault(); // <== disable form submit


  var user = firebase.auth().currentUser;

if (user.emailVerified) {
  // email is verified.
  console.log("email is verified")
} else {
  // email is not verified.
  console.log("email is not verified")
}
  
  });

I want that my Website sends an email to the user after the user enters his email and password.

In my Code, user.sendEmailVerification(); works fine and the user gets an email.

If the user verifies his email and clicks the btn2, the console should print “email is verified”, but this doesn’t happen. The console always prints “email is not verified”. I tried also the firebase.auth().onAuthStateChanged method, but it’s the same.

var user = firebase.auth().currentUser;
 firebase.auth().onAuthStateChanged(user => {
      if(user.emailVerified){ 
        console.log('email is verified')
       }else{
           console.log('email not verified')
       }
    })

Advertisement

Answer

Verifying the email address happens out-of-band, typically in another tab of the same browser or in another application altogether. This means that your application code isn’t immediately made aware of the update to the user’s profile, but only once one of these things happens:

  1. Their ID token is auto-refreshed, which happens every hour.
  2. When the user logs out and logs in again.
  3. When you force to refresh the ID token in your application code by calling reload on their user profile.

Once any of these happens, the user profile will contain the latest information from the server, including the user’s email verification state.

Also see:

Advertisement