Skip to content Skip to sidebar Skip to footer

Firebase - How To Check Whether A User Has Already Signed Up Using Phone Number

I am using firebase phone Authentication . When a user creates a account using phone number and next time he creates account with same phone number Than I want to show a message

Solution 1:

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:

enter image description here

And your piece of code should looks similar to:

On the piece of code of successful login/user registration:

DatabaseRefuserRef= FirebaseDatabase.getInstance.getRef("users");
userRef.orderByChild("telNum").equalTo(phoneNumber).addListenerForSingleValueEvent(newValueEventListener() {

     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();
     }
}

Solution 2:

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(newValueEventListener() {

            @OverridepublicvoidonDataChange(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()) {
                            UserbasicInfouser= userSnapshot.getValue(UserbasicInfo.class);
                              Toast.makeText(UserInfoActivity.this, "User already exist...!", Toast.LENGTH_SHORT).show();

                        }

                    }
                    isExist = true;
                } else {
                    isExist = false;
                }
            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        });

        /*if not exist add data to firebase*/Runnablerunnable=newRunnable() {
            @Overridepublicvoidrun() {
                Log.d(TAG, "isExist: " + isExist);
                if (!isExist) {
                    addDataToDB(false);
                } else {
                    addDataToDB(true);

                }
            }
        };
        newHandler().postDelayed(runnable, 5000);

Solution 3:

onTask result check FirebaseAuthUserCollisionException

if (task.getException() instanceof FirebaseAuthUserCollisionException) {
    Toast.makeText(Signup.this, "User already exist.",  Toast.LENGTH_SHORT).show();
}

Solution 4:

For the solution,

After signup please make some entry in Database which makes an identity of the user, so next time you can identify user already signup.

After OTP verification check in RealTime database already Mobile number exists then so its already otherwise do an entry of that particular mobile number.

Solution 5:

DatabaseReferenceuserRef= FirebaseDatabase.getInstance().getReference("Users");
userRef
    .orderByChild("phonenumber")
    .equalTo(mMobile.getText().toString())
    .addListenerForSingleValueEvent(newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue() != null) {
                //it means user already registered//Add code to show your prompt
                OpenErrorAlrt("Mobile Number already registed");
            } else {
                Intentintent=newIntent(getApplicationContext(), OTPVerifyActivity.class);
                intent.putExtra("phonenumber", mMobile.getText().toString());
                startActivity(intent);
            }

        }

        @OverridepublicvoidonCancelled(DatabaseError databaseError) {

        }
    });

Post a Comment for "Firebase - How To Check Whether A User Has Already Signed Up Using Phone Number"