0

How to sign out the user when the email is not verified, Firebase Auth, or make an error when trying to sing in and the email is not verified?

I use 'com.google.firebase:firebase-auth:19.4.0' Androidx minSdkVersion 21 targetSdkVersion 30 buildToolsVersion "30.0.2"

SingIn_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = email_login.getText().toString().trim();
                String password = password_login.getText().toString().trim();

                if (TextUtils.isEmpty(email)) {
                    email_login.setError("حقل البريد الالكتروني فارغ");
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    password_login.setError("حقل كلمة المرور فارغ");
                    return;
                }

                if (password.length() < 6) {
                    password_login.setError("يجب ان تكون كلمة المرور اكثر من 6 حروف");
                    return;
                }

                progressBar.setVisibility(View.VISIBLE);


                mFirebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(SingInActivity.this, "تم تسجيل الدخول", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(), MainActivity.class));
                            finish();
                            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                        } else {
                            Toast.makeText(SingInActivity.this, "خطأ :" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.GONE);
                        }
                    }
                });
            }
        });

Please Help And Thank You All

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
bart mine
  • 89
  • 8

1 Answers1

2

You'll want to check if the user has verified their email address with:

mFirebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            Toast.makeText(SingInActivity.this, "تم تسجيل الدخول", Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);

            if (!task.getResult().getUser().isEmailVerified()) {
                ...
            }

        }
    }
});

If you want to sign the user out, that'd be task.getResult().getUser().signOut().

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807