问题描述
我在我的应用程序中使用Firebase身份验证,用户可以在其中使用电子邮件&密码,他必须验证电子邮件.
I am using Firebase Authentication in my app where the users can register using Email & Password and he has to verify the email.
用户收到验证邮件,但是当他验证邮件并返回到应用程序时,isEmailVerified()始终为false.因此,尽管我已经验证了电子邮件,但我的应用仍然不允许用户使用所有功能.
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.
但是,如果他们注销并再次登录,则isEmailVerified()立即返回true.但是注销用户并重新登录不是很好.
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");
}}}
推荐答案
在给出一些逻辑之后,这是我的答案
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");
}
}
});
}
这篇关于Firebase电子邮件验证未更新状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!