我目前正在开发一个Android应用程序,该应用程序将Firebase(实时功能)用作后端服务.另外,我已经开发了此功能,如下所示.
private void checkUserExisting (){
Dataa = mRef.getReference().child("Users");
Dataa.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()){
CreateUserProfile();
}else {
final String Uname = name.getText().toString().trim();
final String Email = emailAddress.getText().toString().trim();
Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();
while (dataSnapshots.hasNext())
{
DataSnapshot dataSnapshotChild = dataSnapshots.next();
String EmailAddress = dataSnapshotChild.child("Email").toString();
String UserName = dataSnapshotChild.child("UserName").toString();
if (Uname.equals(UserName) || Email.equals(EmailAddress)){
Toast.makeText(Sgin_Up.this,"This user already exists",Toast.LENGTH_LONG).show();
break;
}else {
CreateUserProfile();
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
最佳答案
创建新帐户或注册过程时,您可以检查用户是否已经存在,例如:
.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
boolean isNewUser = task.getResult().getAdditionalUserInfo().isNewUser();
//retruns true if user is new
//false if user exits,move on login screen/forgot password etc.
}
})
并且您已经阻止了使用相同电子邮件地址创建帐户,这实际上使不必要的检查数据库中是否存在电子邮件地址,因为无法再使用相同电子邮件地址创建新帐户.
databaseReference.orderByChild("Email").equalTo(email).addListenerForSingleValueEvent....