0

Edit - a key thing that I didn't realise at when writing is that this happens when you use Google as an authentication provider

I've set up my Android app so that I can log in and out using Firebase Authentication.

But when I log in, I'm not presented with any options that would let me log in as a different use - it just auto-logs me into the same account I logged into last time.

How can I switch the user account I use to log in?

Code from my main Activity:

    // Authentication stuff
    private lateinit var auth: FirebaseAuth
    private val btnSignIn: Button by lazy { findViewById(R.id.btnSignIn) }
    val signInLauncher = registerForActivityResult(FirebaseAuthUIActivityResultContract()) { res ->
        if (res.resultCode == RESULT_OK) {
            refreshSigninInfo()
        } else {
            showToast("Sign in failed - do you have internet connection?")
        }
    }
    private fun refreshSigninInfo() {  // Called in onCreate() 
        auth = FirebaseAuth.getInstance()
        auth.currentUser?.let {
            btnSignIn.text = "Signed in as ${it.email}\nTap to sign out"
            btnSignIn.setOnClickListener {
                auth.signOut()
                refreshSigninInfo()
            }
        } ?: run {
            btnSignIn.text = "Tap to sign in"
            btnSignIn.setOnClickListener {
                val signInIntent = AuthUI.getInstance()
                    .createSignInIntentBuilder()
                    .setAvailableProviders(arrayListOf(AuthUI.IdpConfig.EmailBuilder().build(), AuthUI.IdpConfig.GoogleBuilder().build()))
                    .build()
                signInLauncher.launch(signInIntent)
                // HERE, upon launch, this this should give the user 
                // the option of selecting a different sign-in account
                // but instead, if the user has previously signed in, 
                // it just directly signs the user in without the option 
                // to change account
            }
        }
    }
Peter
  • 12,274
  • 9
  • 71
  • 86
  • 1
    What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Dec 13 '22 at 07:53
  • Thank Alex, I added a comment to the code explaining where the issue is. There's no error - it's just I'm trying to get it to let me choose a different user when I signed in on a device that had a previous sign in. Currently it just directly signs me in without allowing the switch. – Peter Dec 13 '22 at 14:40
  • 1
    Are you sure signed out from Google first? – Alex Mamo Dec 14 '22 at 06:29
  • I'm sure I've signed out because I've called `auth.signOut()` and `auth.currentUser` is now `null`. – Peter Dec 14 '22 at 19:18
  • 1
    So it turns out you're right Alex, I hadn't signed out of Google. For reasons I don't understand the normal approach of signing out of google client wasn't working either - the solution I posted below ended up working. – Peter Dec 14 '22 at 20:24
  • 1
    If you want to switch to a custom implementation and stop using a library, then this [resource](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-google-one-tap-in-jetpack-compose-60b30e621d0d) will help. Here is the corresponding [repo](https://github.com/alexmamo/FirebaseSignInWithGoogle). – Alex Mamo Dec 15 '22 at 07:04

1 Answers1

0

Finally got it thanks to this answer.

Key thing was to sign out with:

AuthUI.getInstance().signOut(this).addOnCompleteListener {
    refreshSigninInfo()
}

Instead of auth.signOut(). This way you get signed out of both Google and the firebase auth, so you aren't automatically logged in next time through Google.

Full Code (in my main activity - includes some references to buttons and stuff that you won't have):

    // Authentication stuff
    private lateinit var auth: FirebaseAuth
    private val textLoginInfo: TextView by lazy { findViewById(R.id.textLoginInfo) }
    private val btnSignIn: ImageButton by lazy { findViewById(R.id.btnSignIn) }
    val signInLauncher = registerForActivityResult(FirebaseAuthUIActivityResultContract()) { res ->
        if (res.resultCode == RESULT_OK) {
            refreshSigninInfo()
        } else {
            showToast("Sign in failed - do you have internet connection?")
        }
    }
    private fun refreshSigninInfo() {  // Called in onCreate()
        auth = FirebaseAuth.getInstance()
        auth.currentUser?.let {
            btnSignIn.setImageResource(R.drawable.logout_white_24dp)
            textLoginInfo.text = "Signed in: ${it.email}"
            btnSignIn.setOnClickListener {
                AuthUI.getInstance().signOut(this).addOnCompleteListener {
                    refreshSigninInfo()
                }
            }
        } ?: run {
            btnSignIn.setImageResource(R.drawable.login_white_24dp)
            textLoginInfo.text = "Not Signed in"
            btnSignIn.setOnClickListener {
                val signInIntent = AuthUI.getInstance()
                    .createSignInIntentBuilder()
                    .setAlwaysShowSignInMethodScreen(true)
                    .setAvailableProviders(arrayListOf(AuthUI.IdpConfig.EmailBuilder().build(), AuthUI.IdpConfig.GoogleBuilder().build()))
                    .build()
                signInLauncher.launch(signInIntent)
            }
        }
    }
Peter
  • 12,274
  • 9
  • 71
  • 86