How to implement existing user sign-in just using phone number and otp using Firebase

451 Views Asked by At

So, I am trying to create a sign-in and sign-up flow using firebase with primary requirement using phone number only.

But I found that I am only able to sing up the user using createUserWithEmailAndPassword only. Now the issue is how do I sign up the user using phone number.

If not then how does firebase sign in user using phone number?

after entering the phone number and otp two case

  1. if the user already exist he goes to his dashboard
  2. if he is new to app he goes to signup detail page and then to the dashboard
1

There are 1 best solutions below

0
Sisinordi On

The easiest way to add phone number sign-in to your app is to use FirebaseUI, which includes a drop-in sign-in widget that implements sign-in flows for phone number sign-in, as well as password-based and federated sign-in.

Hopefully, You have added the Firebase Dependencies and Enabled Authentication. After this this code will guide you :

  PhoneAuthOptions options = 
  PhoneAuthOptions.newBuilder(mAuth) 
      .setPhoneNumber(phoneNumber)       // Phone number to verify
      .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
      .setActivity(this)                 // Activity (for callback binding)
      .setCallbacks(mCallbacks)          // OnVerificationStateChangedCallbacks
      .build();
  PhoneAuthProvider.verifyPhoneNumber(options);   

If you want to redirect users after Successful Verification, then Firebase comes preloaded with the methods required:

@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
    // This callback will be invoked in two situations:
    // 1 - Instant verification. In some cases the phone number can be instantly
    //     verified without needing to send or enter a verification code.
    // 2 - Auto-retrieval. On some devices Google Play services can automatically
    //     detect the incoming verification SMS and perform verification without
    //     user action.
    Log.d(TAG, "onVerificationCompleted:" + credential);

    signInWithPhoneAuthCredential(credential);
}

@Override
public void onVerificationFailed(FirebaseException e) {
    // This callback is invoked in an invalid request for verification is made,
    // for instance if the the phone number format is not valid.
    Log.w(TAG, "onVerificationFailed", e);

    if (e instanceof FirebaseAuthInvalidCredentialsException) {
        // Invalid request
        // ...
    } else if (e instanceof FirebaseTooManyRequestsException) {
        // The SMS quota for the project has been exceeded
        // ...
    }

    // Show a message and update the UI
    // ...
}

@Override
public void onCodeSent(@NonNull String verificationId,
                       @NonNull PhoneAuthProvider.ForceResendingToken token) {
    // The SMS verification code has been sent to the provided phone number, we
    // now need to ask the user to enter the code and then construct a credential
    // by combining the code with a verification ID.
    Log.d(TAG, "onCodeSent:" + verificationId);

    // Save verification ID and resending token so we can use them later
    mVerificationId = verificationId;
    mResendToken = token;

    // ...
}