Sign up & Login with Phone Number in Firebase

Viewed 211

I am new in Android and I have few questions about when a new user account is created on phone authentication. I created a Register Activity and OTP Activity for user registration. After the user registration, I get the user's information and 5 Activity later I store the datas to Firebase. I can see the user's details and phone number in Firebase.

This is my OTP page:

  private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        
                        Intent intent = new Intent(B_Verification_Activity.this, C_Name_Activity.class);
                        intent.putExtra("userPhone", comp_phoneNumber);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                    }
                    else{
                        return;
                    }
                }
            });
}

And this is my last activity that I store the datas in Firebase:

  public void storeNewUserData() {
    rootNode = FirebaseDatabase.getInstance();
    String userId = firebaseAuth.getCurrentUser().getUid();
    reference = rootNode.getInstance().getReference().child("Kullanıcılar").child(user_Gender.toString()).child(userId);

    UserHelperClass addNewUser = new UserHelperClass(user_phoneNumber, user_FirstName, user_Gender, user_Birthdate, user_Hobbies);
    reference.setValue(addNewUser);
}

But I don't know how user to login with phone number. In registration page, I just verified phone number, that's it. I don't know user is created or not in Register Activity. I want to create user in OTP activity. Is there any other code to I should write?

My english is not very good, thank you for helping.

1 Answers

There is no registration or login functionality in Firebase Phone Authentication. Actually, they just take user's phone number and verify it with a one time password. It is phone number verification actually. In other terms, if the phone number is new, then a new user account is created after verification. But if a returning user verifies his/her number, they are simply signed in. You might want to do one thing from here :
You may want to check if user is new or old and then get data from Realtime database/Firestore if they are returning users. You might be also sending data of new user to database, in that case you can send their data if they are first time user. To help you with this, there is a function in Firebase Android SDK named isNewUser() which returns boolean value true if they are verifying (logging) with their number for first time. You may read about that function in detail from here.

Related