1

I have a function that enable user to sign up or create an account. It also sends verification after signing up. I only want users whose email were verified to logged in.What would I do?

This is my login function.

   function signIn() {
   var email = $("#emailtxt").val();
   var password = $("#passwordtxt").val();
   var auth = firebase.auth();        

   firebase.auth().signInWithEmailAndPassword(email, 
   password).then(function(value) {
      //NEED TO PULL USER DATA?                     

      var user = firebase.auth().currentUser;
      if(user){
        console.log(user.uid);
        $("#popup").click();
      }
    }).catch(function(error) {
   // Handle Errors here.
   var errorCode = error.code;
   var errorMessage = error.message;
   if (errorCode === 'auth/wrong-password') {
     alert('Wrong password.');
   } else {
    alert(errorMessage);
  }
  console.log(error);
});
     }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

I hope you can console.log your current user

var user = firebase.auth().currentUser;
console.log(user); //there you can see emailVerified boolean object will be available.
if(user.emailVerified){
   // then you can allow your users to be logged in your site.

}
else if(!user.emailVerified){ //send a emailverification mail to the user
  firebase.auth().currentUser.sendEmailVerification().then(function() {
    alert('Email Verification Sent!');
  });

}

added a picture of user mail verified or not

MuruGan
  • 1,402
  • 2
  • 11
  • 23
  • Yes, I can see now in the console if the user is verified or not and disabled the user to login if its not verified. Thank you by the way. You're a great help. – Camille Enolpe Sep 14 '17 at 02:07