I am creating an app on android and at the moment because my knowledge is really basic, I have already created several things, but I have an inconvenience, it turns out that I am doing the user registration and what happens is that I wanted to validate if a mail was already registered and I already achieved it only now when it registers a new user error in the app and despite everything if you register
annex my code
final String password = mcontraseña.getText().toString().trim();
final String confirm = mconfirma.getText().toString().trim();
final String name = mNombre.getText().toString().trim();
final String ciudad = mciudad.getText().toString().trim();
final String direccion = mdireccion.getText().toString().trim();
final String documento = mdocumento.getText().toString().trim();
final String numero = mnumero.getText().toString().trim();
final String departamento = mdepartamento.getText().toString().trim();
final String email = mEmailFiedl.getText().toString().trim();
mAuth2.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
final boolean[] flag = {false};
for (DataSnapshot IDGenerado : snapshot.getChildren()) {
String usernameTaken = IDGenerado.child("correo").getValue(String.class);
if (email.equals(usernameTaken.toLowerCase())) {
flag[0] = true; // Indica que ya fue tomado el username
}
}
// si el flag nunca se marco como verdadero, entonces no se ha tomado el username
if (flag[0] != false) {
mEmailFiedl.setError("Este correo ya se encuentra Registrado");
} else {
if (Objects.equals(password, confirm)) {
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Ingresa Tu correo!", Toast.LENGTH_SHORT).show();
return;
}
if (!validarEmail(email)) {
mEmailFiedl.setError("Email no válido");
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "No Has Ingresado una contraseña!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "La Contraseña Debe ser mayor De 6 caracteres!", Toast.LENGTH_SHORT).show();
return;
}
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(registro.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(registro.this, "Tu Usuario Ha sido creado", Toast.LENGTH_SHORT).show();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
DatabaseReference currentUserDB = mDatabase.child(mAuth.getCurrentUser().getUid());
currentUserDB.child("Nombre").setValue(name);
currentUserDB.child("ciudad").setValue(ciudad);
currentUserDB.child("correo").setValue(email);
currentUserDB.child("documento").setValue(documento);
currentUserDB.child("numero").setValue(numero);
currentUserDB.child("direccion").setValue(direccion);
currentUserDB.child("departamento").setValue(departamento);
currentUserDB.child("image").setValue("default");
if (!task.isSuccessful()) {
Toast.makeText(registro.this, "Error no hemos podido registrarte" + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(registro.this, principal.class));
finish();
}
}
});
} else {
mconfirma.setError("Las Contraseñas no son iguales");
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
and another small part of the code '
mAuth2 = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();'
I appreciate your help and collaboration