Home » Android » java – Firebase: How to check if user email is verified-Exceptionshub

java – Firebase: How to check if user email is verified-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am trying to check if the user email has been verified. However, I am not sure where to add this part of code in.

FirebaseUser user = mAuth.getCurrentUser();
                if(!user.isEmailVerified()){//check if the email is verified

                Toast.makeText(MainActivity.this,"Please verify your email",Toast.LENGTH_SHORT).show();
                }

When I add it into AuthListener and signInWithEmailAndPassword, I get “Attempt to invoke virtual method ‘boolean com.google.firebase.auth.FirebaseUser.isEmailVerified()’ on a null object reference” error

        firebaseAuthListener = new FirebaseAuth.AuthStateListener() {//if the login status change
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser users = FirebaseAuth.getInstance().getCurrentUser();

                if (users!=null){
                    Intent intent = new Intent(MainActivity.this,MapActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }
            }
        };
mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if(!task.isSuccessful()){
                            Toast.makeText(MainActivity.this,"Wrong email or password", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

Thank you for your help.

How to&Answers: