Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase confirmation email not being sent

I've set up Firebase email/password authentication successfully, but for security reasons I want the user to confirm her/his email. It says on Firebases website:

When a user signs up using an email address and password, a confirmation email is sent to verify their email address.

But when I sign up, I doesn't receive a confirmation email.

I've looked and can only find a code for sending the password reset email, but not a code for sending the email confirmation.

I've looked here:

https://firebase.google.com/docs/auth/ios/manage-users#send_a_password_reset_email

anyone got a clue about how I can do it?

like image 588
Benja0906 Avatar asked May 25 '16 08:05

Benja0906


People also ask

Why do verification emails take so long to arrive?

Your email might be delayed if your email provider is putting incoming emails through a lot of filters to filter out spam. These filters can sometimes take a long time to complete (depending on the internet provider).

How does Firebase email verification work?

You can use Firebase Authentication to sign in a user by sending them an email containing a link, which they can click to sign in. In the process, the user's email address is also verified. There are numerous benefits to signing in by email: Low friction sign-up and sign-in.

How do I change my Firebase verification email?

To customize your Firebase project's email action handler, you must create and host a web page that uses the Firebase JavaScript SDK to verify the request's validity and complete the request. Then, you must customize your Firebase project's email templates to link to your custom action handler.


3 Answers

I noticed that the new Firebase email authentication docs is not properly documented.

firebase.auth().onAuthStateChanged(function(user) {
  user.sendEmailVerification(); 
});

Do note that:

  1. You can only send email verification to users object whom you created using Email&Password method createUserWithEmailAndPassword
  2. Only after you signed users into authenticated state, Firebase will return a promise of the auth object.
  3. The old onAuth method has been changed to onAuthStateChanged.

To check if email is verified:

firebase.auth().onAuthStateChanged(function(user) { 
  if (user.emailVerified) {
    console.log('Email is verified');
  }
  else {
    console.log('Email is not verified');
  }
});
like image 91
Xavier J. Wong Avatar answered Oct 08 '22 11:10

Xavier J. Wong


After creating a user a User object is returned, where you can check if the user's email has been verified or not.

When a user has not been verified you can trigger the sendEmailVerification method on the user object itself.

firebase.auth()
    .createUserWithEmailAndPassword(email, password)
    .then(function(user){
      if(user && user.emailVerified === false){
        user.sendEmailVerification().then(function(){
          console.log("email verification sent to user");
        });
      }
    }).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;

      console.log(errorCode, errorMessage);
    });

You can also check by listening to the AuthState, the problem with the following method is, that with each new session (by refreshing the page), a new email is sent.

firebase.auth().onAuthStateChanged(function(user) {
  user.sendEmailVerification(); 
});
like image 13
tdhulster Avatar answered Oct 08 '22 10:10

tdhulster


You can send verification email and check if was verified as follow into the AuthListener:

mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (user != null) {

//---- HERE YOU CHECK IF EMAIL IS VERIFIED

                if (user.isEmailVerified()) {
                    Toast.makeText(LoginActivity.this,"You are in =)",Toast.LENGTH_LONG).show();
                } 

                else {

//---- HERE YOU SEND THE EMAIL

                    user.sendEmailVerification();
                    Toast.makeText(LoginActivity.this,"Check your email first...",Toast.LENGTH_LONG).show();
                }

            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // [START_EXCLUDE]
            updateUI(user);
            // [END_EXCLUDE]
        }
    };
like image 5
Pablo Prado Avatar answered Oct 08 '22 11:10

Pablo Prado