Firebase email verification not updating status

泄露秘密 提交于 2019-12-24 01:18:53

问题


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:


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");
                    }

                }
            });

        }


来源:https://stackoverflow.com/questions/48530554/firebase-email-verification-not-updating-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!