So the issue I am having is I have a registration form in which users enter information (which also contains an email).
I have an onClick method called
public void onCreateAccount(View view);
when the user clicks "Register", it validates the fields on the form.
public class Foo extends AppCompatActivity {
//OTHER PRIVATE MEMBERS
private EditText etEmail;
boolean isValid;
private DatabaseReference databaseUser = FirebaseDatabase.getInstance().getReference().child("User");
@Override
protected void onCreate(Bundle savedInstanceState) {
//DOES SOME INITIALIZATION
}
public void onCreateAccount(View view){
String email = etEmail.getText().toString().trim();
if(validateEmail(email)){
String id = databaseUser.push().getKey();
User user = new User(id, email);
databaseUser.child(user.getId()).setValue(user);
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.putExtra("isCreateAccount", true);
startActivityForResult (intent,0);
}
}
private boolean validateEmail(String email) {
isValid = true;
databaseUser.orderByChild("email").equalTo(emailUserEntered).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
isValid=false;
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Before inserting the record into the firebase database, I want to first check if the email already exists prior to inserting. So a person typing email = a@mail.com would not allow so.