1

What actually i am doing here, When a user login to their account, Then i am checking that whatever they had verified their email address or not, If not then i am starting the EmailVerificationActivity. From where when user click on SEND VERIFICATION EMAIL Button an Email Verification code will be sent to user's email address.

User is sending email verification link

after that when user successfully verified their email address, when they click SEND VERIFICATION LINK Button again.

Instead of showing toast message Toast.makeText(this, "Your email has been verified, Now you can login.", Toast.LENGTH_LONG).show();, sending the Email Verification link again.

sending email verification link again

Why isEmailVerified() returning the false condition.

This is my EmailVerificationActivity

package com.socialcodia.sherewatan;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class EmailVerificationActivity extends AppCompatActivity {

    private TextView tvEmailAddress;
    private Button btnSendVerificationEmail, btnSignOut;

    //Firebase
    FirebaseAuth mAuth;
    FirebaseUser mUser;

    String email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email_verification);

        //Init
        tvEmailAddress = findViewById(R.id.tvEmailAddress);
        btnSendVerificationEmail = findViewById(R.id.btnSendVerificationEmail);
        btnSignOut = findViewById(R.id.btnSignOut);

        //Firebase Init
        mAuth = FirebaseAuth.getInstance();
        mUser = mAuth.getCurrentUser();

        // get and set email address
        email = mUser.getEmail();
        tvEmailAddress.setText(email);

        btnSignOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signOut();
            }
        });

        btnSendVerificationEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isEmailVerified();
            }
        });
    }

    private void isEmailVerified()
    {
        if (mAuth.getCurrentUser()!=null)
        {
            boolean isEmailVerified = mAuth.getCurrentUser().isEmailVerified();
            if (isEmailVerified)
            {
                Toast.makeText(this, "Your email has been verified, Now you can login.", Toast.LENGTH_LONG).show();
            }
            else
            {
                sendVerificationEmail();
            }
        }
    }

    private void sendVerificationEmail()
    {
        mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful())
                {
                    Toast.makeText(EmailVerificationActivity.this, "Email verification link has been sent", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(EmailVerificationActivity.this, "Oops! Failed to send email verification link", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void sendToLoginWithEmail()
    {
        Intent intent= new Intent(getApplicationContext(),LoginActivity.class);
        intent.putExtra("email",email);
        startActivity(intent);
        finish();
    }

    private void signOut()
    {
        mAuth.signOut();
        sendToLoginWithEmail();
    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
mufazmi
  • 1,103
  • 4
  • 18
  • 37

3 Answers3

2

You need to reload current user information. You see the data of the user your have from authentication are old and you need to retrieve the latest. Use the reload() method for that.

Chris Papantonis
  • 720
  • 7
  • 18
1

How i solved the error.

private void isEmailVerified()
{
    mAuth.getCurrentUser().reload().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            if (mAuth.getCurrentUser()!=null)
            {
                boolean isEmailVerified = mAuth.getCurrentUser().isEmailVerified();
                if (isEmailVerified)
                {
                    Toast.makeText(getApplicationContext(), "Your email has been verified, Now you can login.", Toast.LENGTH_LONG).show();
                }
                else
                {
                    sendVerificationEmail();
                }
            }
        }
    });
}
mufazmi
  • 1,103
  • 4
  • 18
  • 37
0

I used the following line to solve this problem

await FirebaseAuth.instance.currentUser!.reload();

24Doggi
  • 59
  • 3