Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send verification email with Firebase?

I am signing up my users using Firebase's email and password method. like this:

mAuth.createUserWithEmailAndPassword(email, password)

.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {

    if (task.isSuccessful()) {

        FirebaseUser signed = task.getResult().getUser();

        writeNewUser(signed.getUid());

        new android.os.Handler().postDelayed(

                new Runnable() {
                    public void run() {

                        updateUser(b);

                    }
                }, 3000);

    } else {

        new android.os.Handler().postDelayed(

                new Runnable() {
                    public void run() {

                        onSignupFailed();

                    }
                }, 3000);

    }

    }
});

After the user's email has been successfully registered, I would like Firebase to send a verification email. I know this is possible using Firebase's sendEmailVerification. In addition to sending this email, I want the user's account to be disabled until they verify the email. This would also require using Firebase's isEmailVerified feature. However, I have been unsuccessful in getting Firebase to send the verification email, I have not been able to figure out to get it to disable and enable the account sending the verification email and after it has been verified.

like image 303
amigo Avatar asked Nov 03 '16 14:11

amigo


People also ask

Does Firebase charge for email verification?

So you don't pay for verifying an email address, nor for each API call you make, but only for completed verifications through the code that was sent in the SMS message.

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

This question is about how to use Firebase to send the verification email. The OP is unable to figure out how to disable and enable the account sending the verification email and after it has been verified.

Also, this is not properly documented in the firebase documentation. So I am writing a step by step procedure that someone may follow if he/she is facing the problem.

1) User can use createUserWithEmailAndPassword method.

Example:

mAuth.createUserWithEmailAndPassword(email, password)                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {                     @Override                     public void onComplete(@NonNull Task<AuthResult> task) {                         Log.d("TAG", "createUserWithEmail:onComplete:" + task.isSuccessful());                          // If sign in fails, display a message to the user. If sign in succeeds                         // the auth state listener will be notified and logic to handle the                         // signed in user can be handled in the listener.                         if (!task.isSuccessful()) {                             // Show the message task.getException()                         }                         else                         {                             // successfully account created                             // now the AuthStateListener runs the onAuthStateChanged callback                         }                          // ...                     }                 }); 

If the new account was created, the user is also signed in, and the AuthStateListener runs the onAuthStateChanged callback. In the callback, you can manage the work of sending the verification email to the user.

Example:

onCreate(...// mAuthListener = new FirebaseAuth.AuthStateListener() {     @Override     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {         FirebaseUser user = firebaseAuth.getCurrentUser();         if (user != null) {             // User is signed in             // NOTE: this Activity should get onpen only when the user is not signed in, otherwise             // the user will receive another verification email.             sendVerificationEmail();         } else {             // User is signed out          }         // ...     } }; 

Now the send verification email can be written like:

private void sendVerificationEmail()     {         FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();          user.sendEmailVerification()                 .addOnCompleteListener(new OnCompleteListener<Void>() {                     @Override                     public void onComplete(@NonNull Task<Void> task) {                         if (task.isSuccessful()) {                             // email sent                                       // after email is sent just logout the user and finish this activity                                     FirebaseAuth.getInstance().signOut();                                     startActivity(new Intent(SignupActivity.this, LoginActivity.class));                                     finish();                         }                         else                         {                             // email not sent, so display message and restart the activity or do whatever you wish to do                                      //restart this activity                                     overridePendingTransition(0, 0);                                     finish();                                     overridePendingTransition(0, 0);                                     startActivity(getIntent());                          }                     }                 });     } 

Now coming to LoginActivity:

Here if the user is successfully logged in then we can simply call a method where you are writing logic for checking if the email is verified or not.

Example:

mAuth.signInWithEmailAndPassword(email, password)                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {                     @Override                     public void onComplete(@NonNull Task<AuthResult> task) {                         //Log.d("TAG", "signInWithEmail:onComplete:" + task.isSuccessful());                          // If sign in fails, display a message to the user. If sign in succeeds                         // the auth state listener will be notified and logic to handle the                         // signed in user can be handled in the listener.                         if (!task.isSuccessful()) {                             //Log.w("TAG", "signInWithEmail:failed", task.getException());                          } else {                             checkIfEmailVerified();                         }                         // ...                     }                 }); 

Now consider the checkIfEmailVerified method:

private void checkIfEmailVerified() {     FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();      if (user.isEmailVerified())     {         // user is verified, so you can finish this activity or send user to activity which you want.         finish();         Toast.makeText(LoginActivity.this, "Successfully logged in", Toast.LENGTH_SHORT).show();     }     else     {         // email is not verified, so just prompt the message to the user and restart this activity.         // NOTE: don't forget to log out the user.         FirebaseAuth.getInstance().signOut();          //restart this activity      } } 

So here I m checking if the email is verified or not. If not, then log out the user.

So this was my approach to keeping track of things properly.

like image 163
Amit Upadhyay Avatar answered Sep 29 '22 12:09

Amit Upadhyay


send verification to user's Email

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); user.sendEmailVerification(); 

check if user is verified

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); boolean emailVerified = user.isEmailVerified(); 
like image 45
karem Avatar answered Sep 29 '22 14:09

karem


Use FirebaseAuth.getInstance().getCurrentUser().sendEmailVerification() and FirebaseAuth.getInstance().getCurrentUser().isEmailVerified()

There is no way to disable the account via the Firebase SDK. The thing you can do is use the GetTokenResult containing the Firebase Auth ID Token and validate it against your custom backend or set a flag to Firebase database corresponding to that user. Personally I'd go with the flag in the Firebase database

like image 21
Ognian Gloushkov Avatar answered Sep 29 '22 13:09

Ognian Gloushkov