Skip to content

Commit 1eb6996

Browse files
committed
fix(auth): improve user identifier retrieval and manage MFA button visibility
1 parent c5461b6 commit 1eb6996

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

auth/src/main/java/com/firebase/ui/auth/ui/screens/FirebaseAuthScreen.kt

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,10 @@ import androidx.compose.foundation.layout.padding
2828
import androidx.compose.material3.AlertDialog
2929
import androidx.compose.material3.Button
3030
import androidx.compose.material3.CircularProgressIndicator
31-
import androidx.compose.material3.ExperimentalMaterial3Api
3231
import androidx.compose.material3.MaterialTheme
33-
import androidx.compose.material3.PlainTooltip
3432
import androidx.compose.material3.Scaffold
3533
import androidx.compose.material3.Surface
3634
import androidx.compose.material3.Text
37-
import androidx.compose.material3.TooltipAnchorPosition
38-
import androidx.compose.material3.TooltipBox
39-
import androidx.compose.material3.TooltipDefaults
40-
import androidx.compose.material3.rememberTooltipState
4135
import androidx.compose.runtime.Composable
4236
import androidx.compose.runtime.CompositionLocalProvider
4337
import androidx.compose.runtime.LaunchedEffect
@@ -78,6 +72,7 @@ import com.firebase.ui.auth.ui.screens.email.EmailAuthScreen
7872
import com.firebase.ui.auth.ui.screens.phone.PhoneAuthScreen
7973
import com.firebase.ui.auth.util.EmailLinkPersistenceManager
8074
import com.firebase.ui.auth.util.SignInPreferenceManager
75+
import com.firebase.ui.auth.util.displayIdentifier
8176
import com.google.firebase.auth.AuthCredential
8277
import com.google.firebase.auth.AuthResult
8378
import com.google.firebase.auth.MultiFactorResolver
@@ -723,7 +718,6 @@ private fun SuccessDestination(
723718
}
724719
}
725720

726-
@OptIn(ExperimentalMaterial3Api::class)
727721
@Composable
728722
private fun AuthSuccessContent(
729723
authUI: FirebaseAuthUI,
@@ -733,7 +727,7 @@ private fun AuthSuccessContent(
733727
onManageMfa: () -> Unit,
734728
) {
735729
val user = authUI.getCurrentUser()
736-
val userIdentifier = user?.email ?: user?.phoneNumber ?: user?.uid.orEmpty()
730+
val userIdentifier = user?.displayIdentifier().orEmpty()
737731
Column(
738732
modifier = Modifier.fillMaxSize(),
739733
verticalArrangement = Arrangement.Center,
@@ -746,26 +740,9 @@ private fun AuthSuccessContent(
746740
)
747741
Spacer(modifier = Modifier.height(16.dp))
748742
}
749-
if (user != null && authUI.auth.app.options.projectId != null) {
750-
TooltipBox(
751-
positionProvider = TooltipDefaults.rememberTooltipPositionProvider(
752-
TooltipAnchorPosition.Above
753-
),
754-
tooltip = {
755-
PlainTooltip {
756-
Text(stringProvider.mfaDisabledTooltip)
757-
}
758-
},
759-
state = rememberTooltipState(
760-
initialIsVisible = !configuration.isMfaEnabled
761-
)
762-
) {
763-
Button(
764-
onClick = onManageMfa,
765-
enabled = configuration.isMfaEnabled
766-
) {
767-
Text(stringProvider.manageMfaAction)
768-
}
743+
if (user != null && authUI.auth.app.options.projectId != null && configuration.isMfaEnabled) {
744+
Button(onClick = onManageMfa) {
745+
Text(stringProvider.manageMfaAction)
769746
}
770747
Spacer(modifier = Modifier.height(8.dp))
771748
}
@@ -783,7 +760,7 @@ private fun EmailVerificationContent(
783760
onSignOut: () -> Unit,
784761
) {
785762
val user = authUI.getCurrentUser()
786-
val emailLabel = user?.email ?: stringProvider.emailProvider
763+
val emailLabel = user?.email?.takeIf { it.isNotBlank() } ?: stringProvider.emailProvider
787764
Column(
788765
modifier = Modifier.fillMaxSize(),
789766
verticalArrangement = Arrangement.Center,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2025 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.firebase.ui.auth.util
16+
17+
import com.google.firebase.auth.FirebaseUser
18+
19+
/**
20+
* Returns the best available display identifier for the user, trying each field in order:
21+
* email → phoneNumber → displayName → uid.
22+
*
23+
* Each field is checked for blank (not just null) so that an empty string returned by the
24+
* Firebase SDK falls through to the next candidate rather than being displayed as-is.
25+
* [FirebaseUser.uid] is always non-null and non-blank for a signed-in user, so the result
26+
* is guaranteed to be non-blank.
27+
*/
28+
fun FirebaseUser.displayIdentifier(): String =
29+
email?.takeIf { it.isNotBlank() }
30+
?: phoneNumber?.takeIf { it.isNotBlank() }
31+
?: displayName?.takeIf { it.isNotBlank() }
32+
?: uid

e2eTest/src/test/java/com/firebase/ui/auth/ui/screens/MfaDisabledTest.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import android.os.Looper
55
import androidx.compose.material3.Text
66
import androidx.compose.runtime.collectAsState
77
import androidx.compose.runtime.getValue
8+
import androidx.compose.ui.test.assertDoesNotExist
89
import androidx.compose.ui.test.assertIsDisplayed
9-
import androidx.compose.ui.test.assertIsNotEnabled
1010
import androidx.compose.ui.test.junit4.createComposeRule
1111
import androidx.compose.ui.test.onNodeWithText
1212
import androidx.compose.ui.test.performClick
@@ -147,10 +147,9 @@ class MfaDisabledTest {
147147
composeTestRule.waitForIdle()
148148
shadowOf(Looper.getMainLooper()).idle()
149149

150-
// Verify the Manage MFA button is displayed but disabled
150+
// Verify the Manage MFA button is not shown when MFA is disabled
151151
composeTestRule.onNodeWithText(stringProvider.manageMfaAction)
152-
.assertIsDisplayed()
153-
.assertIsNotEnabled()
152+
.assertDoesNotExist()
154153
}
155154

156155
@Test

0 commit comments

Comments
 (0)