Firebase AuthUI : How to register PhoneAuthProvider OnVerificationStateChangedCallbacks

Viewed 62

I have used Firebase AuthUI in my app to login using google, phone and email methods. I need to store data on Firestore after successful authentication.

The code I have used for launching Firebase Auth Screen with diff providers listed :

 startActivityForResult(
     AuthUI.getInstance()
           .createSignInIntentBuilder()
           .setIsSmartLockEnabled(false, true)
           .setLogo(R.drawable.yam_logo_orange_512)
           .setAvailableProviders(Arrays.asList(
               new AuthUI.IdpConfig.EmailBuilder().build(),
               new AuthUI.IdpConfig.GoogleBuilder().build(),
               new AuthUI.IdpConfig.PhoneBuilder().build()
            )).build(),
RC_SIGN_IN);

But, as I wrote down above - I am using AuthUI that provides default buttons and UI - I don't have ownership of the phone number field and same for the phone number.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)    {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
        String providerType =  response.getProviderType();

        if (resultCode == RESULT_OK) {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if (user != null) {
                switch (providerType) {
                    case Constants.PROVIDER_GOOGLE:
                        account = GoogleSignIn.getLastSignedInAccount(this);
                        if (account != null) {
                            firebaseAuthWithGoogle(account, response.isNewUser());
                        }
                        break;

                    case Constants.PROVIDER_PHONE:
                        if(null!=phoneAuthCredential){
                            signInWithPhoneAuthCredential(phoneAuthCredential, response.isNewUser());
                        }
                        break;

                    case Constants.PROVIDER_EMAIL:
                        break;

                }
            }
        }
    }
}

Please help me in completing this. Do I need to make a custom screen to get the number and then register PhoneAuth callback?

Thanks, Kanak

1 Answers

Just make a custom screen (UI) so you have ownership of It all, making it easy to access the text fields, also include some:

if (requestCode == RC_SIGN_IN) {
println(requestCode)
// 
}

here as well:

if (user != null) {
println(user)
//
}

just to further understand where you could be getting errors. Also once you see where your errors are by printing the values you should be able to register the PhoneAuth callback.

Related