Reputation: 2276
I've been trying to get a fix on this situation for days now, and I can't seem to find a solution. So here is what I want to do:
I have an app which uses firebase-ui-auth to authenticate users via email/Google account. As soon as a user is authenticated, I write the user's details to a Firebase database child 'clients'. Now I want to make sure that the next time the same user logs in, his details should not be written to the 'clients'. To do this, I created another child 'reg_clients', to which I push EmailID of the user who has logged in before.
Now, as there was no way in Firebase to search on the server side whether currentUser.getEmail()
already exists in 'reg_clients', I use:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
// user is signed in
clientCompletedRegistrationReference.addListenerForSingleValueEvent(clientCompletedRegistrationValueEventListener);
Log.d("in onActivityResult","user is:"+user.getDisplayName());
Log.d("in onActivityResult","emailAddressList is:"+ Arrays.toString(emailAddressList.toArray()));
Log.d("in onActivityResult","user.getEmail() is"+ emailAddressList.contains(user.getEmail()));
if(emailAddressList.contains(user.getEmail())) {
Toast.makeText(NavDrawerActivity.this, "You're signed in again", Toast.LENGTH_SHORT).show();
} else {
// Gets photoUrl when Google+ photo is set, otherwise returns null. Appending '?sz=100' at end gets a photo of 100x100 px
String photoUrl = user.getPhotoUrl() != null ? user.getPhotoUrl().toString()+"?sz=100": null;
Toast.makeText(NavDrawerActivity.this, "You're signed in for the first time", Toast.LENGTH_SHORT).show();
Client client = new Client(user.getDisplayName(),0,photoUrl,user.getDisplayName().replaceAll("\\s+","").toLowerCase(),"Brooklyn,NY");
// User has just created an account, save his/her details to the 'Clients' child
clientDatabaseReference.push().setValue(client);
// When user is registering for the first time, pushes his/her email ID to the 'Completed Registration' child
clientCompletedRegistrationReference.push().setValue(user.getEmail());
}
return;
}
}
In the database, there is always (initially)
--reg_clients
|
---K_3uY0-M4ZCkRSt53SZ: "[email protected]"
But still the log shows
in onActivityResult user is:XYZ
emailAddressList is:[]
user.getEmail() isfalse
Due to this condition being false the data gets pushed to both the clients
and reg_clients
as new data, and thus multiple entries of the same user is being made in the database.
And then onDataChange
of clientCompletedRegistrationValueEventListener is invoked which populates the emailAddressList
, but it has no purpose now, as duplicate entry has already been made.
Please help me in this approach or please let me know if you have a better way to do this. Thanks
Edit: Added code for clientCompletedRegistrationValueEventListener
clientCompletedRegistrationValueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot emailSnapshot : dataSnapshot.getChildren()) {
Log.d("Adding",emailSnapshot.getValue().toString()+" to emailAddressList");
emailAddressList.add(emailSnapshot.getValue().toString());
Log.d("in onDataChange","emailAddressList is:"+Arrays.toString(emailAddressList.toArray()));
}
}
Upvotes: 0
Views: 105
Reputation: 4074
When you add a listener you don't get the value immediately.You have to wait for the response.So ,Put that code inside onDataChange of the listener.Like this
clientCompletedRegistrationValueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot emailSnapshot : dataSnapshot.getChildren()) {
Log.d("Adding", emailSnapshot.getValue().toString() + " to emailAddressList");
emailAddressList.add(emailSnapshot.getValue().toString());
Log.d("in onDataChange", "emailAddressList is:" + Arrays.toString(emailAddressList.toArray()));
}
Log.d("in onActivityResult","user is:"+user.getDisplayName());
Log.d("in onActivityResult","emailAddressList is:"+ Arrays.toString(emailAddressList.toArray()));
Log.d("in onActivityResult","user.getEmail() is"+ emailAddressList.contains(user.getEmail()));
if(emailAddressList.contains(user.getEmail())) {
Toast.makeText(NavDrawerActivity.this, "You're signed in again", Toast.LENGTH_SHORT).show();
} else {
// Gets photoUrl when Google+ photo is set, otherwise returns null. Appending '?sz=100' at end gets a photo of 100x100 px
String photoUrl = user.getPhotoUrl() != null ? user.getPhotoUrl().toString()+"?sz=100": null;
Toast.makeText(NavDrawerActivity.this, "You're signed in for the first time", Toast.LENGTH_SHORT).show();
Client client = new Client(user.getDisplayName(),0,photoUrl,user.getDisplayName().replaceAll("\\s+","").toLowerCase(),"Brooklyn,NY");
// User has just created an account, save his/her details to the 'Clients' child
clientDatabaseReference.push().setValue(client);
// When user is registering for the first time, pushes his/her email ID to the 'Completed Registration' child
clientCompletedRegistrationReference.push().setValue(user.getEmail());
}
}
};
Upvotes: 1