Firebase - How to check whether a user has already signed up using Phone Number
I have done this into my project and it is working perfectly, you can use this, you can use your phone number instead of "deviceId".
mFirebaseDatabaseRefrence.orderByChild("deviceId").equalTo(deviceId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
Log.d(TAG, "Datasnap Shots: " + dataSnapshot.getValue());
/* if alredy exist and check for first time, second time isExist=true*/
if (!isExist) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
UserbasicInfo user = userSnapshot.getValue(UserbasicInfo.class);
Toast.makeText(UserInfoActivity.this, "User already exist...!", Toast.LENGTH_SHORT).show();
}
}
isExist = true;
} else {
isExist = false;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
/*if not exist add data to firebase*/
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.d(TAG, "isExist: " + isExist);
if (!isExist) {
addDataToDB(false);
} else {
addDataToDB(true);
}
}
};
new Handler().postDelayed(runnable, 5000);
In order to detect whether that phone number has already been used for account registration, you can't only rely on the default authentication table. But also has to use the Firebase database to create a Dummy user table for checking.
For example, you can create a json tree to save user data in the realtime database in to something structured like this:
And your piece of code should looks similar to:
On the piece of code of successful login/user registration:
DatabaseRef userRef = FirebaseDatabase.getInstance.getRef("users");
userRef.orderByChild("telNum").equalTo(phoneNumber).addListenerForSingleValueEvent(new ValueEventListener() {
if (dataSnapshot.getValue() != null){
//it means user already registered
//Add code to show your prompt
showPrompt();
}else{
//It is new users
//write an entry to your user table
//writeUserEntryToDB();
}
}