Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email not being Verified in Firebase

I am using Firebase's isEmailVerified method to verify an email. The following is the code:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verifying);
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    mAuth=FirebaseAuth.getInstance();
    spinner=(ProgressBar)findViewById(R.id.progressBar);
    spinner.setVisibility(View.GONE);
    Log.e("I am launched","hello");
    if(user.isEmailVerified()==true){
       Log.e("I am here","hello");
        State state= new AccountSettingUp(this);
       state.doAction();
   } else {
        Log.e("Maybe i am here","yes");
       user.sendEmailVerification()
               .addOnCompleteListener(new OnCompleteListener<Void>() {
                   @Override
                   public void onComplete(@NonNull Task<Void> task) {
                       if (task.isSuccessful()) {
                           Log.d("email", "Email sent.");
                       }
                   }
               });
   }

Here, the else code should only run when the email is not verified. However, even after I verify the email and restart the activity, the if statement is not true and the email is sent again.

like image 802
Fahad Saleem Avatar asked Dec 04 '16 21:12

Fahad Saleem


2 Answers

This behavior appears to be a limitation, possibly a bug, in the current version (10.0.1) of Firebase Authentication. The same issue is reported in this related question.

I tried doing a reload() of the user data after the email verification. That didn't help. As reported in the related question, it seems that a sign-out/sign-in is required to get the new email verification status.

like image 95
Bob Snyder Avatar answered Nov 03 '22 10:11

Bob Snyder


For firebase-auth:10.0.1, it seems that it is not possible. However, the workaround is to log the user out and logging them in again. Upon signing in, the isEmailVerified() function will work properly.

I did a sign up activity

    mAuth.createUserWithEmailAndPassword(Email, Password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()){
                            user = mAuth.getCurrentUser();
                            UserProfileChangeRequest profileChange = new UserProfileChangeRequest.Builder()
                                    .setDisplayName(FirstName)
                                    .build();

                            user.updateProfile(profileChange).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    if(task.isSuccessful()){
                                        Toast.makeText(getBaseContext(), "Thanks for signing up, " + FirstName + "! Please verify your email!", Toast.LENGTH_SHORT).show();
                                        user.sendEmailVerification();
                                        mAuth.signOut();
                                        Intent a = new Intent(getBaseContext(),Login_Activity.class);
                                        startActivity(a);
                                    }
                                    else {
                                        Toast.makeText(getBaseContext(),"Failed to create username",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });

                        }
                        else{
                            Toast.makeText(getBaseContext(),"Failed",Toast.LENGTH_SHORT).show();
                        }
                    }
                });

As seen in the 'task.isSuccessful()' portion of the '.updateProfile' code, I did an 'mAuth.signOut();' and informing them to verify their email before sending them back to the login screen.

On the Login screen, I did a simple 'if (user.isEmailVerified())' statement to check for verification. If they are still not verified, a toast will appear to remind them, if they are verified then they will proceed into the next activity.

Hope this helps!

like image 31
NewToProgramming1989 Avatar answered Nov 03 '22 09:11

NewToProgramming1989