Firebase电子邮件验证未更新状态

时间:2018-01-30 21:10:01

标签: android firebase firebase-authentication

我在我的应用中使用Firebase身份验证,用户可以使用Email& amp;密码,他必须验证电子邮件。

用户获取验证邮件,但当他验证并返回应用程序时,isEmailVerified()始终为false。所以我的应用程序仍然不允许用户使用所有功能,尽管他已经验证了他的电子邮件。

但如果他们注销并再次登录,则isEmailVerified()会立即返回true。但是退出用户并再次登录是不好的。

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 个答案:

答案 0 :(得分:2)

这是我提出一些逻辑后的答案

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

                }
            });

        }