Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should EmailAlreadyInUseException just above get the same isEmailProviderConfigured() check? A Google sign-in collision lands there through AuthException.from, and it still opens the email step. Happy for this to be a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d6377db, with a test for the address-in-use path when no Email provider is configured.

{
pendingLinkingCredential.value = exception.credential
navigateToEmailStep(AuthRoute.Email.SignIn(), exception.email)
}
} else {
null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,21 @@ fun EmailAuthScreen(
onNotificationConsumed: (() -> Unit)? = null,
content: @Composable ((EmailAuthContentState) -> Unit)? = null,
) {
val provider = configuration.providers.filterIsInstance<AuthProvider.Email>().first()
val stringProvider = LocalAuthUIStringProvider.current
val provider = configuration.providers.filterIsInstance<AuthProvider.Email>().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("") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ fun SignInEmailLinkUI(
onNavigateBack: (() -> Unit)? = null,
isEmailLocked: Boolean = false,
) {
val provider = configuration.providers.filterIsInstance<AuthProvider.Email>().first()
// Nothing to render without an Email provider.
val provider = configuration.providers.filterIsInstance<AuthProvider.Email>().firstOrNull()
?: return
val stringProvider = LocalAuthUIStringProvider.current
val emailValidator = remember { EmailValidator(stringProvider) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ fun SignUpUI(
onNavigateBack: (() -> Unit)? = null,
isEmailLocked: Boolean = false,
) {
val provider = configuration.providers.filterIsInstance<AuthProvider.Email>().first()
// Nothing to render without an Email provider.
val provider = configuration.providers.filterIsInstance<AuthProvider.Email>().firstOrNull()
?: return
val context = LocalContext.current
val stringProvider = LocalAuthUIStringProvider.current
val displayNameValidator = remember { GeneralFieldValidator(stringProvider) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we swap these two imports so EmailAuthMode comes before EmailAuthScreen?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d6377db.

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
Expand Down Expand Up @@ -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<AuthException>()
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
Expand Down Expand Up @@ -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()
)
Expand All @@ -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<String>) = AuthUITransitions(
transitionSpec = {
into += "transition:${label()}"
Expand Down
Loading