0

I'm trying to check if the user's email is existing in my Firebase Realtime Database. I used Firebase Authenticate to get user's Google Play Email, and if the user's email is not existing on my Firebase, the user will be prompted to another activity. Here's my code:

MainActivity.class

// Let's declare Firebase
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private FirebaseUser mUser;

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    mUser = mAuth.getInstance().getCurrentUser();

    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String dEmail = dataSnapshot.getValue().toString();
            String gEmail = mUser.getEmail();
            for(DataSnapshot data: dataSnapshot.getChildren()){
                if(data.child("email").child(EncodeString(gEmail)).exists()) {
                    Toast.makeText(getApplicationContext(), "There's one: " + EncodeString(gEmail), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "No email: " + EncodeString(gEmail), Toast.LENGTH_LONG).show();
                }
            }
            /*
            if(dEmail == gEmail) {
                startActivity(new Intent(getApplicationContext(), RegisterActivity.class));
                finish();
            } else {
                Toast.makeText(getApplicationContext(), "No email", Toast.LENGTH_LONG).show();
            }*/
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

public static String EncodeString(String string) {
    return string.replace(".", ",");
}

public static String DecodeString(String string) {
    return string.replace(",", ".");
}

Here's the structure of my Firebase Realtime Database

Here's the structure of my Firebase Realtime Database

UPDATE: The question is how can I check if the email (the email is the one in my google play which is sholomon12@g..) is exisiting in my database. Note: I got the same email inserted, I just replaced the one in database with comma since I can't insert dot in firebase database

Mon Pinoliad
  • 67
  • 1
  • 2
  • 11

1 Answers1

1

There is a workaround here when creating users in a Firebase database. Instead of using that random generated id provided by the push() method, use the email address. Your database structure should look like this:

Firebase-root
    |
    --- users
         |
         --- john@email,com
                 |
                 --- //user details

Now to check the users for existens, you can use exists() method on DatabaseReference object. So to chieve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("users").child("john@email,com");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()) {
            //do something
        } else {
            //do something else
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Yes you're right. Email can also become user's unique ID. And I'd rather use it instead of push() this time. Thank you Alex! – Mon Pinoliad Feb 21 '18 at 01:55