Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether the Register Email is verified or not in Firebase?

onSubmit(formData) {
if(formData.valid) {
  console.log(formData.value);
  this.af.auth.createUser({
    email: formData.value.email,
    password: formData.value.password
  }).then(
    authState => {
    authState.auth.sendEmailVerification();
    this.router.navigate(['/login'])
  }).catch(
    (err) => {
    console.log(err);
    this.error = err;
  })
}
}

In Firebase, I set the SendEmailVerfication like the code above, and the email could send normally.However, in my app, there is no difference between the user who does not click the verification email with those clicked, how to make a difference?

like image 346
pyy Avatar asked Apr 01 '17 17:04

pyy


People also ask

How do I know if my firebase email is verified?

Check verification statusinitializeApp(config); firebase. auth(). onAuthStateChanged( function(user) { if(user){ var emailVerified = user. emailVerified; var email = user.

How do I know if an account is signed in firebase Authentication?

var user = firebase. auth(). currentUser; if (user) { // User is signed in. } else { // No user is signed in. }

How does firebase email Authentication work?

Email and password based authenticationAuthenticate users with their email addresses and passwords. The Firebase Authentication SDK provides methods to create and manage users that use their email addresses and passwords to sign in. Firebase Authentication also handles sending password reset emails.


3 Answers

You can use firebase.auth().currentUser.emailVerified

This will return true or false.

like image 114
Yohan E Avatar answered Nov 26 '22 13:11

Yohan E


- If you are already loggedIn below solutions can help you to check email verified status.

1) The recommended way to get the current user is by setting an observer on the Auth object:

 firebase.auth().onAuthStateChanged(authUser => {
      if(authUser.user.emailVerified){ //This will return true or false
        console.log('email is verified')
       }else{
           console.log('email not verified')
       }
    })

2) You can also get the currently signed-in user by using the currentUser property.

var user = firebase.auth().currentUser;

if (user.emailVerified) {
  // email is verified.
} else {
  // email is not verified.
}

- If you are not loggedIn then please try below solution.

     firebase.auth().signInWithEmailAndPassword(email, password ).then(authUser => {

      if(authUser.user.emailVerified){ //This will return true or false
       console.log('email is verified')
      }else{
        console.log('email not verified')
      }
      }).catch(function(error) {

    });
like image 21
Maheshvirus Avatar answered Nov 26 '22 13:11

Maheshvirus


According to the documentation, the User object contains an emailVerified property.

So the user to which the signInWithEmailAndPassword method's promise resolves - or the user that is passed to the onAuthStateChanged method's callback - can be inspected and the value of emailVerified can be checked.

like image 27
cartant Avatar answered Nov 26 '22 13:11

cartant