diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreen.kt b/auth/src/main/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreen.kt index 94bd9efbc..3be90a12a 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreen.kt +++ b/auth/src/main/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreen.kt @@ -93,6 +93,7 @@ import com.firebase.ui.auth.ui.screens.email.EmailAuthContentState import com.firebase.ui.auth.ui.screens.email.EmailAuthMode import com.firebase.ui.auth.ui.screens.email.emailAuthDestinations import com.firebase.ui.auth.ui.screens.email.isEmailLinkSignInOffered +import com.firebase.ui.auth.ui.screens.email.isEmailProviderConfigured import com.firebase.ui.auth.ui.screens.email.isEmailSignUpOffered import com.firebase.ui.auth.ui.screens.email.navigateToEmailStep import com.firebase.ui.auth.ui.screens.mfa.MfaChallengeScreen @@ -923,14 +924,25 @@ fun FirebaseAuthScreen( } } + // The only recovery for these two is the email sign-in step. Without + // an Email provider there is no such step, so offer nothing rather + // than a step this configuration cannot show. is AuthException.EmailAlreadyInUseException -> { - { navigateToEmailStep(AuthRoute.Email.SignIn(), exception.email) } + if (configuration.isEmailProviderConfigured()) { + { navigateToEmailStep(AuthRoute.Email.SignIn(), exception.email) } + } else { + null + } } is AuthException.AccountLinkingRequiredException -> { - { - pendingLinkingCredential.value = exception.credential - navigateToEmailStep(AuthRoute.Email.SignIn(), exception.email) + if (configuration.isEmailProviderConfigured()) { + { + pendingLinkingCredential.value = exception.credential + navigateToEmailStep(AuthRoute.Email.SignIn(), exception.email) + } + } else { + null } } diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/EmailAuthDestinations.kt b/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/EmailAuthDestinations.kt index ebe556025..4b8936095 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/EmailAuthDestinations.kt +++ b/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/EmailAuthDestinations.kt @@ -227,6 +227,13 @@ internal fun AuthUIConfiguration.isEmailStepOffered(step: AuthRoute.Email.Step): is AuthRoute.Email.SignIn, is AuthRoute.Email.ResetPassword -> true } +/** + * Whether the configuration includes an [AuthProvider.Email] at all. A recovery that would move + * the user into an email step asks this first: without one there is no email step to show. + */ +internal fun AuthUIConfiguration.isEmailProviderConfigured(): Boolean = + providers.any { it is AuthProvider.Email } + /** * Whether the email flow may offer account creation. False while reauthenticating — proving an * existing identity can never end in a new account — and whenever the configuration or the email diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/EmailAuthScreen.kt b/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/EmailAuthScreen.kt index 680e622f1..889849434 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/EmailAuthScreen.kt +++ b/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/EmailAuthScreen.kt @@ -209,8 +209,21 @@ fun EmailAuthScreen( onNotificationConsumed: (() -> Unit)? = null, content: @Composable ((EmailAuthContentState) -> Unit)? = null, ) { - val provider = configuration.providers.filterIsInstance().first() val stringProvider = LocalAuthUIStringProvider.current + val provider = configuration.providers.filterIsInstance().firstOrNull() + if (provider == null) { + // Nothing to render without an Email provider. Report the misconfiguration and leave the + // step, so a host that routes here anyway neither crashes nor strands the user on it. + LaunchedEffect(Unit) { + onError( + AuthException.MisconfigurationException( + message = stringProvider.unknownErrorRecoveryMessage + ) + ) + onCancel() + } + return + } val coroutineScope = rememberCoroutineScope() val displayNameValue = rememberSaveable { mutableStateOf("") } diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/SignInEmailLinkUI.kt b/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/SignInEmailLinkUI.kt index c8482e58b..eb38adf9f 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/SignInEmailLinkUI.kt +++ b/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/SignInEmailLinkUI.kt @@ -75,7 +75,9 @@ fun SignInEmailLinkUI( onNavigateBack: (() -> Unit)? = null, isEmailLocked: Boolean = false, ) { - val provider = configuration.providers.filterIsInstance().first() + // Nothing to render without an Email provider. + val provider = configuration.providers.filterIsInstance().firstOrNull() + ?: return val stringProvider = LocalAuthUIStringProvider.current val emailValidator = remember { EmailValidator(stringProvider) } diff --git a/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/SignUpUI.kt b/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/SignUpUI.kt index 0c08be1c6..fc36e456a 100644 --- a/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/SignUpUI.kt +++ b/auth/src/main/java/com/firebase/ui/auth/ui/screens/email/SignUpUI.kt @@ -71,7 +71,9 @@ fun SignUpUI( onNavigateBack: (() -> Unit)? = null, isEmailLocked: Boolean = false, ) { - val provider = configuration.providers.filterIsInstance().first() + // Nothing to render without an Email provider. + val provider = configuration.providers.filterIsInstance().firstOrNull() + ?: return val context = LocalContext.current val stringProvider = LocalAuthUIStringProvider.current val displayNameValidator = remember { GeneralFieldValidator(stringProvider) } diff --git a/auth/src/test/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreenEmailRecoveryTest.kt b/auth/src/test/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreenEmailRecoveryTest.kt index ab7efb410..d1a9095c6 100644 --- a/auth/src/test/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreenEmailRecoveryTest.kt +++ b/auth/src/test/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreenEmailRecoveryTest.kt @@ -24,6 +24,7 @@ import androidx.compose.animation.EnterTransition import androidx.compose.animation.ExitTransition import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.SideEffect import androidx.compose.ui.Modifier @@ -47,8 +48,11 @@ import com.firebase.ui.auth.configuration.AuthUITransitions import com.firebase.ui.auth.configuration.authUIConfiguration import com.firebase.ui.auth.configuration.auth_provider.AuthProvider import com.firebase.ui.auth.configuration.string_provider.DefaultAuthUIStringProvider +import com.firebase.ui.auth.configuration.string_provider.LocalAuthUIStringProvider import com.firebase.ui.auth.mfa.MfaChallengeContentState import com.firebase.ui.auth.ui.FirebaseAuthTestTags +import com.firebase.ui.auth.ui.screens.email.EmailAuthMode +import com.firebase.ui.auth.ui.screens.email.EmailAuthScreen import com.google.common.truth.Truth.assertThat import com.google.firebase.FirebaseApp import com.google.firebase.FirebaseOptions @@ -779,6 +783,101 @@ class FirebaseAuthScreenEmailRecoveryTest { composeTestRule.onNodeWithText(OTHER_EMAIL).assertIsDisplayed() } + /** + * The recovery is gated on an Email provider being configured, not on sign-up being offered: + * linking signs in to an account that already exists, so an app that turns off new accounts + * still needs the step. + */ + @Test + fun `an account that needs linking still recovers to sign-in when sign-up is not offered`() { + start(configuration = emailAndPhoneConfiguration(isNewAccountsAllowed = false)) + + recoverFrom(accountLinkingRequired(mock(AuthCredential::class.java))) + + composeTestRule.onNodeWithTag(FirebaseAuthTestTags.SignIn.EMAIL_FIELD).assertIsDisplayed() + composeTestRule.onNodeWithText(OTHER_EMAIL).assertIsDisplayed() + } + + /** + * firebase/FirebaseUI-Android#2550: a provider sign-in hit an existing account, and the + * dialog's recovery opened an email step the app never configured, which crashed. Without an + * Email provider there is no step to offer, so the dialog offers none. + */ + @Test + fun `no recovery is offered for an account that needs linking when no Email provider is configured`() { + start(configuration = noEmailConfiguration()) + + composeTestRule.runOnIdle { + authUI.updateAuthState( + AuthState.Error(accountLinkingRequired(mock(AuthCredential::class.java))) + ) + } + composeTestRule.waitForIdle() + + composeTestRule.onNodeWithText(stringProvider.errorDialogTitle).assertExists() + composeTestRule.onNodeWithTag(FirebaseAuthTestTags.ErrorRecovery.RETRY_BUTTON) + .assertDoesNotExist() + composeTestRule.onNodeWithTag(FirebaseAuthTestTags.SignIn.EMAIL_FIELD).assertDoesNotExist() + } + + /** + * The same crash reached another way: a Google sign-in collision arrives as + * `EmailAlreadyInUseException` through `AuthException.from`, and its recovery is the same + * email step the configuration cannot show. + */ + @Test + fun `no recovery is offered for an address already in use when no Email provider is configured`() { + start(configuration = noEmailConfiguration()) + + composeTestRule.runOnIdle { + authUI.updateAuthState( + AuthState.Error( + AuthException.EmailAlreadyInUseException( + message = "already in use", + email = TYPED_EMAIL, + ) + ) + ) + } + composeTestRule.waitForIdle() + + composeTestRule.onNodeWithText(stringProvider.errorDialogTitle).assertExists() + composeTestRule.onNodeWithTag(FirebaseAuthTestTags.ErrorRecovery.RETRY_BUTTON) + .assertDoesNotExist() + composeTestRule.onNodeWithTag(FirebaseAuthTestTags.SignIn.EMAIL_FIELD).assertDoesNotExist() + } + + /** + * `EmailAuthScreen` is public, so a host can route to it with a configuration that has no + * Email provider. Rather than throwing, or leaving the user on an empty step, it reports the + * misconfiguration and asks the host to leave. + */ + @Test + fun `the email screen reports a misconfiguration and leaves without an Email provider`() { + val errors = mutableListOf() + var cancelled = 0 + composeTestRule.setContent { + CompositionLocalProvider(LocalAuthUIStringProvider provides stringProvider) { + EmailAuthScreen( + context = applicationContext, + configuration = noEmailConfiguration(), + authUI = authUI, + onSuccess = {}, + onError = { errors += it }, + onCancel = { cancelled++ }, + mode = EmailAuthMode.SignIn, + onNavigateToMode = { _, _ -> }, + ) + } + } + composeTestRule.waitForIdle() + + composeTestRule.onNodeWithTag(FirebaseAuthTestTags.SignIn.EMAIL_FIELD).assertDoesNotExist() + assertThat(errors).hasSize(1) + assertThat(errors.single()).isInstanceOf(AuthException.MisconfigurationException::class.java) + assertThat(cancelled).isEqualTo(1) + } + /** * The point of the recovery, and the half a landing assertion cannot see: the credential the * failure carried has to survive the move and be linked once the password sign-in it asked for @@ -929,12 +1028,15 @@ class FirebaseAuthScreenEmailRecoveryTest { this.transitions = transitions } - private fun emailAndPhoneConfiguration(): AuthUIConfiguration = authUIConfiguration { + private fun emailAndPhoneConfiguration( + isNewAccountsAllowed: Boolean = true, + ): AuthUIConfiguration = authUIConfiguration { context = applicationContext providers { provider( AuthProvider.Email( isDisplayNameRequired = false, + isNewAccountsAllowed = isNewAccountsAllowed, emailLinkActionCodeSettings = null, passwordValidationRules = emptyList() ) @@ -950,6 +1052,25 @@ class FirebaseAuthScreenEmailRecoveryTest { isCredentialManagerEnabled = false } + /** + * Two providers and neither is Email, so the flow opens on the method picker: where a + * provider sign-in such as Facebook raises its account-linking failure. + */ + private fun noEmailConfiguration(): AuthUIConfiguration = authUIConfiguration { + context = applicationContext + providers { + provider(AuthProvider.Anonymous) + provider( + AuthProvider.Phone( + defaultNumber = null, + defaultCountryCode = null, + allowedCountries = null + ) + ) + } + isCredentialManagerEnabled = false + } + private fun recordingTransitions(into: MutableList) = AuthUITransitions( transitionSpec = { into += "transition:${label()}"