29
votes

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.

6

6 Answers

52
votes

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.

7
votes

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();
6
votes

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

3
votes

For sending email link with Firebase first you need to grab FirebaseAuth instance using the instance we create user on Firebase through:

firebaseauth.createUserWithEmailAndPassword(email,pass);

When method return success we send verification link to user using Firebase user instance as follows:

 final FirebaseUser user = mAuth.getCurrentUser();
                      user.sendEmailVerification()

See this link: https://technicalguidee.000webhostapp.com/2018/10/email-verification-through-link-using-firebase-authentication-product-android.

1
votes
   mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if(task.isSuccessful()){

                    mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {

                            if (task.isSuccessful()) {



                                Toast.makeText(this, "please check email for verification.", Toast.LENGTH_SHORT).show();
                                loadingBar.dismiss();
                            }else{
                                Toast.makeText(this, task.getException().getMessage() , Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
0
votes

For Kotlin

val user: FirebaseUser? = firebaseAuth.currentUser
user?.sendEmailVerification()