Loren
Loren

Reputation: 892

Firebase AuthUI not calling onActivityResult

I'm building an Android app that uses Firebase AuthUI to handle user login.

implementation 'com.google.firebase:firebase-firestore:21.4.3'
implementation 'com.firebaseui:firebase-ui-auth:6.2.0'
implementation 'com.firebaseui:firebase-ui:6.2.0'

When my activity starts and checks if the user is signed in... and if not it calls startActivityForResult. See code below.

When AuthUI completes it never calls onActivityResult. Instead the startup activity's onRestart() is called.

Any suggestions as to how I can get onActivityResult called?

   private fun startFirebaseAuthActivity() {
        Timber.d("startFirebaseAuthActivity(): ")
        EventBus.getDefault().post(MyEvents.Companion.StopListFragment())
        // Choose authentication providers
        val providers = arrayListOf(
            AuthUI.IdpConfig.EmailBuilder().build(),
            AuthUI.IdpConfig.GoogleBuilder().build()
        )

        // Create and launch sign-in intent
        startActivityForResult(
            AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setIsSmartLockEnabled(false)
                .setAvailableProviders(providers)
                .build(),
            RC_SIGN_IN
        )
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Timber.d("onActivityResult(): ")

        if (requestCode == RC_SIGN_IN) {
            val response = IdpResponse.fromResultIntent(data)

            if (resultCode == Activity.RESULT_OK && response != null) {
                // Successfully signed in
                auth = FirebaseAuth.getInstance()
                newUser = User(auth!!.currentUser!!)
                isNewUser = response.isNewUser
                Timber.d("onActivityResult(): isNewUser=$isNewUser")
                if (isNewUser) {
                    createNewUser()
                } 

            } else {
                // Sign in failed.
                // If response is null the user canceled the sign-in flow using the back button.
                // Otherwise check response.getError().getErrorCode() and handle the error.

                when {
                    response == null -> {
                        //If no response from the Server
                        Timber.d("onActivityResult(): sign_in_cancelled.")
                        showSnackbar(R.string.sign_in_cancelled)
                    }

                    response.error?.errorCode == ErrorCodes.NO_NETWORK -> {
                        //If there was a network problem the user's phone
                        Timber.d("onActivityResult(): no_internet_connection")
                        showSnackbar(R.string.no_internet_connection)
                    }

                    response.error?.errorCode == ErrorCodes.UNKNOWN_ERROR -> {
                        //If the error cause was unknown
                        Timber.e("onActivityResult(): unknown_error")
                        showSnackbar(R.string.unknown_error)

                    }
                    else -> {

                        Timber.e("onActivityResult(): unknown_sign_in_response")
                        showSnackbar(R.string.unknown_sign_in_response) //if the sign in response was unknown
                    }
                }
                startFirebaseAuthActivity()
            }
        }
    }

Upvotes: 1

Views: 898

Answers (1)

Jason Ozias
Jason Ozias

Reputation: 378

I was having this same issue with code similar to the following

    if (token?.isNotEmpty() == true) {
        val activityIntent = Intent(this, MainActivity::class.java)
        startActivity(activityIntent)
    } else {
        val loginIntent = AuthUI.getInstance()
            .createSignInIntentBuilder()
            .setAvailableProviders(providers)
            .build()
        startActivityForResult(loginIntent, RC_SIGN_IN)
    }
    finish()

The issue was the finish outside of the if else block. This was finishing my activity before the result could be returned from the AuthUI activity. Moving the finish up to the token case solved the problem for me

    if (token?.isNotEmpty() == true) {
        val activityIntent = Intent(this, MainActivity::class.java)
        startActivity(activityIntent)
        finish()
    } else {
        val loginIntent = AuthUI.getInstance()
            .createSignInIntentBuilder()
            .setAvailableProviders(providers)
            .build()
        startActivityForResult(loginIntent, RC_SIGN_IN)
    }
    

I didn't see a finish() in your code, but maybe something similar is completing the activity before the result can be returned.

Upvotes: 0

Related Questions