Home > Software design >  How i can get "true" in isEmailVerified()?
How i can get "true" in isEmailVerified()?

Time:01-05

I try to write my messenger. I need to register new user in FirebaseAuth. For that, i need to get "true" in isEmailVerified(). My programm sends emailverification to current user. It means that, current user is not null, but in next line i call isEmailVerified() and it returns false, even when email is verified.

mAuth.createUserWithEmailAndPassword(edEmail.getText().toString(), edPassword.getText().toString())
    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {

                FirebaseUser currentUser = mAuth.getCurrentUser();
                if (currentUser != null) {
                    sendEmailVerify(currentUser);

                    if (currentUser.isEmailVerified()) {
                        StartActivity(MainActivity.class);
                    }
                }else{
                    Toast.makeText(RegActivity.this, "Error", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(RegActivity.this, "email or password error", Toast.LENGTH_SHORT).show();
            }
        }
    });
private void sendEmailVerify(FirebaseUser currentUser) {
    currentUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {

            } else {

            }
        }
    });
}
public void StartActivity(Class activity) {
    Intent intent = new Intent(RegActivity.this, activity);
    startActivity(intent);
    RegActivity.this.finish();
}

My program must wait until the moment your email will be verified.

CodePudding user response:

Sending a verification email does not immediately set the email address as verified on a user profile. After all, the mail could bounce or it could be sent to a mailbox that the user doesn't own. Only once the user clicks the link in the email, does Firebase mark their email address as verified.

Since this click on the link and subsequent web page load happens outside of your application, your application code also doesn't get notified when it happens. Instead, the profile in your application only gets the updated information when:

  • The user signs out and in again.
  • Every hour, when the ID token is automatically refreshed.
  • When you force the SDK to refresh the ID token, for example by [calling reload() on the current user.

That last option gives you the most flexibility, as you can determine in your application code when to force this refresh. Common ways to go about this:

  • Reload the token when the app/activity becomes active, which typically will happen automatically after they've switched to their email client and back.
  • Show a button to the user to force the reload.
  • Force reload the token periodically, say every few seconds, while the app is active and waiting for the user to click the link. Consider implementing exponential back-off here though, as you may otherwise hit rate limits.

For the best experience, you may considering multiple or all of these.

  • Related