firebase / FirebaseUI-Android

Optimized UI components for Firebase
https://firebaseopensource.com/projects/firebase/firebaseui-android/
Apache License 2.0
4.64k stars 1.83k forks source link

Developer error using Microsoft authentication provider #2166

Open MartonPRez opened 4 months ago

MartonPRez commented 4 months ago

Step 1: Are you in the right place?

Step 2: Describe your environment

Step 3: Describe the problem:

Getting com.firebase.ui.auth.FirebaseUiException: Developer error after going through authentication with Microsoft auth provider.

The error comes from ProviderUtils.java:229. NOTE: My assumption is that this usecase happens, since the email I'm using xyz@mycompany.com is already authenticated with Google as a provider. The web AuthUI library handles this by asking giving the user the option to link the accounts. Is this what we don't have on Android, and if so, how do we handle this manually?

The setup in the firebase console, and the mobile client should be good, based on the facts that:

  1. Google authentication works on mobile as expected (suggesting the correct config is used)
  2. Microsoft authentication works for our webapp using the same project (suggesting the Firebase Console setup is correct)

Observed Results:

onActivityResult is called after the Custom Google Chrome tab is closed with resultCode=0, and info in data.extras: IdpResponse{mUser=null, mToken='null', mSecret='null', mIsNewUser='false', mException=com.firebase.ui.auth.FirebaseUiException: Developer error, mPendingCredential=null}

Expected Results:

onActivityResult called with a successful resultCode=-1, authentication succeeds.

Relevant Code:

val intent = when (providerType) {
    LoginProviderType.GOOGLE -> viewModel.getGoogleLoginIntent(email)
    LoginProviderType.MICROSOFT -> viewModel.getMicrosoftLoginIntent(email)
    else -> return@observeFreshly
}
startActivityForResult(intent, REQUEST_CODE)

-----

override fun getMicrosoftLoginIntent(email: String?): Intent {
        val providerBuilder = AuthUI.IdpConfig.MicrosoftBuilder()
        if (!email.isNullOrBlank()) {
            providerBuilder.setCustomParameters(
                mapOf("login_hint" to email)
            )
        }
        return AuthUI.getInstance()
            .createSignInIntentBuilder()
            .setLogo(R.drawable.logo)
            .setTheme(R.style.MyStyle)
            .setAvailableProviders(arrayListOf(providerBuilder.build()))
            .setIsSmartLockEnabled(false)
            .build()
    }

----

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            viewModel.onLoginFlowDone()
        }
    }
MartonPRez commented 4 months ago

I've figured out what is happening, but not closing the issue to see if there is any workaround for this usecase.

Since our application requires us to use to use email+password login with a given tenant, and any other authentication methods without a tenant, we can't use the complete UI solution this library provides. Our workaround was to launch dedicated intents, one per auth provider. This caused the error, since when the library tries to link two accounts from different providers, both providers needs to be set as allowedProviders, which is not the case our aforementioned setup.