我尝试检查连接到Firebase的登录屏幕中的错误,我使用的OnCompleteListener
检查是否:
1)用户已经注册
2)如果用户在我的数据库中提供了信息
每当用户输入未经过身份验证的电子邮件时,它也会告诉他提供信息(检查是否存在空的唯一ID),但它不应该发生,或者换句话说,为什么if(task.isComplete() && !task.isSuccessful())
没有唯一ID且没有用户
这里是OnCompleteListener
代码,忽略OnSuccesslistener
一个
Task<AuthResult> authResultTask = firebaseAuth.signInWithEmailAndPassword(Email, Password)
// if login is complete
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// dismisses progressbar and checks if the users has already given Essential Information,
// If not it will take him to the essential information activity
progressDialog.dismiss();
if(task.isComplete() && !task.isSuccessful()){
// Checks if user has submitted information in the Essential Information activity
// Takes the Unique ID(if it is present if not it will tell him to sign up or invalid email)
// asks the firebase database if he has given information to the database
FirebaseUser user = firebaseAuth.getCurrentUser();
String UserUID = user.getUid();
reference.child("Users").child(UserUID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// User exists
if (dataSnapshot.exists()) {
//Displays Toast telling user that their information is saved in the database
Toast.makeText(LogInActivity.this, "You have data in our database ", Toast.LENGTH_SHORT).show();
}
//User doesn't have information in the database
else {
// Displays Toast telling user he/she needs to sign in into the firebase database
// User goes to UserInformationActivity to give his/her information
Toast.makeText(LogInActivity.this, "You need to give Essential Information", Toast.LENGTH_SHORT).show();
// 3 second delay
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Goes to UserInformationActivity
Intent GoToUserInformation = new Intent(LogInActivity.this, UserInformationActivity.class);
LogInActivity.this.startActivity(GoToUserInformation);
}
}, 3000);
}
}
// if the checking got cancelled, likability of that happening is small
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
if (!task.isSuccessful()) {
try {
throw task.getException();
} catch(FirebaseAuthInvalidUserException e) {
Toast.makeText(LogInActivity.this, "Invalid email, You need to sign up first", Toast.LENGTH_SHORT).show();
// 3 second delay
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Goes to UserInformationActivity
Intent Signup = new Intent(LogInActivity.this, SignupActivity.class);
LogInActivity.this.startActivity(Signup);
}
}, 3000);
} catch(Exception e) {
Toast.makeText(LogInActivity.this, "", Toast.LENGTH_SHORT).show();
Log.e("Exception", e.getMessage());
}
}
}
})
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Toast.makeText(LogInActivity.this, "SuccesListener working", Toast.LENGTH_SHORT).show();
}
});