4

I am using Firebase Authentication in my app where the users can register using Email & Password and he has to verify the email.

The user gets the verification mails, but when he verifies it and comes back to the app, the isEmailVerified() is always false. So my app still doesn't allow the user to use all functions in spite of the fact that he has verified his email.

But if they log out and login again, the isEmailVerified() returns true immediately. But is it not good to log out the user and login back again.

public class Profile extends AppCompatActivity {

    FirebaseDatabase database;
    DatabaseReference myRef;
    TextView name;
    Button logout;
    FirebaseAuth auth;
    String userStatus;

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

        auth = FirebaseAuth.getInstance();
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("name");

        name=findViewById(R.id.id_name);
        logout=findViewById(R.id.id_logout);

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                auth.signOut();

               startActivity(new Intent(Profile.this, Login.class));
            }
        });

  userStatus= String.valueOf(auth.getCurrentUser().isEmailVerified());

        if (userStatus =="true")

        {

            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    String value = dataSnapshot.getValue(String.class);
                    name.setText("Hello my name is: "+value);
                    Log.d("ashu", "Value is: " + value);
                }

                @Override
                public void onCancelled(DatabaseError error) {
                    // Failed to read value
                    Log.d("ashu", "Failed to read value.", error.toException());
                }
            });
        }

        else {
            Toast.makeText(Profile.this,"Verify your email ", Toast.LENGTH_SHORT).show();
            name.setText("Verify your email");

       }}}

1 Answers1

3

Here is my answer after putting some logic

userStatus = String.valueOf(auth.getCurrentUser().isEmailVerified());

// user has not verified the email
Toast.makeText(Profile.this,"Verify your email ", Toast.LENGTH_SHORT).show();
name.setText("Verify your email");

auth.getCurrentUser().reload().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {

                    if (userStatus =="true")

                    {
//if they have verified the email
                        myRef.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                // retrieving the value of current user

                                String value = dataSnapshot.getValue(String.class);
                                name.setText("Hello my name is: "+value);
                            }

                            @Override
                            public void onCancelled(DatabaseError error) {
                                // Failed to read value
                                Log.d("ashu", "Failed to read value.", error.toException());
                            }
                        });



                    }

                    else {

                        name.setText("Verify your mail");
                    }

                }
            });

        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459