Skip to content

Commit 60edc2d

Browse files
authored
fix(auth): improve user identifier retrieval (#2314)
* fix(auth): improve user identifier retrieval * updates
1 parent c5461b6 commit 60edc2d

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

app/src/main/java/com/firebaseui/android/demo/HighLevelApiDemoActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import com.firebase.ui.auth.configuration.theme.AuthUITheme
4242
import com.firebase.ui.auth.ui.screens.AuthSuccessUiContext
4343
import com.firebase.ui.auth.ui.screens.FirebaseAuthScreen
4444
import com.firebase.ui.auth.util.EmailLinkConstants
45+
import com.firebase.ui.auth.util.displayIdentifier
46+
import com.firebase.ui.auth.util.getDisplayEmail
4547
import com.google.firebase.auth.actionCodeSettings
4648

4749
class HighLevelApiDemoActivity : ComponentActivity() {
@@ -211,7 +213,7 @@ private fun AppAuthenticatedContent(
211213
when (state) {
212214
is AuthState.Success -> {
213215
val user = uiContext.authUI.getCurrentUser()
214-
val identifier = user?.email ?: user?.phoneNumber ?: user?.uid.orEmpty()
216+
val identifier = user.displayIdentifier()
215217
Column(
216218
modifier = Modifier.fillMaxSize(),
217219
horizontalAlignment = Alignment.CenterHorizontally,
@@ -263,7 +265,7 @@ private fun AppAuthenticatedContent(
263265
}
264266

265267
is AuthState.RequiresEmailVerification -> {
266-
val email = uiContext.authUI.getCurrentUser()?.email ?: stringProvider.emailProvider
268+
val email = uiContext.authUI.getCurrentUser().getDisplayEmail(stringProvider.emailProvider)
267269
Column(
268270
modifier = Modifier.fillMaxSize(),
269271
horizontalAlignment = Alignment.CenterHorizontally,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ import com.firebase.ui.auth.ui.screens.email.EmailAuthScreen
7878
import com.firebase.ui.auth.ui.screens.phone.PhoneAuthScreen
7979
import com.firebase.ui.auth.util.EmailLinkPersistenceManager
8080
import com.firebase.ui.auth.util.SignInPreferenceManager
81+
import com.firebase.ui.auth.util.displayIdentifier
82+
import com.firebase.ui.auth.util.getDisplayEmail
8183
import com.google.firebase.auth.AuthCredential
8284
import com.google.firebase.auth.AuthResult
8385
import com.google.firebase.auth.MultiFactorResolver
@@ -733,7 +735,7 @@ private fun AuthSuccessContent(
733735
onManageMfa: () -> Unit,
734736
) {
735737
val user = authUI.getCurrentUser()
736-
val userIdentifier = user?.email ?: user?.phoneNumber ?: user?.uid.orEmpty()
738+
val userIdentifier = user.displayIdentifier()
737739
Column(
738740
modifier = Modifier.fillMaxSize(),
739741
verticalArrangement = Arrangement.Center,
@@ -783,7 +785,7 @@ private fun EmailVerificationContent(
783785
onSignOut: () -> Unit,
784786
) {
785787
val user = authUI.getCurrentUser()
786-
val emailLabel = user?.email ?: stringProvider.emailProvider
788+
val emailLabel = user.getDisplayEmail(stringProvider.emailProvider)
787789
Column(
788790
modifier = Modifier.fillMaxSize(),
789791
verticalArrangement = Arrangement.Center,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
* Returns an empty string if the user is null.
26+
*/
27+
fun FirebaseUser?.displayIdentifier(): String =
28+
this?.email?.takeIf { it.isNotBlank() }
29+
?: this?.phoneNumber?.takeIf { it.isNotBlank() }
30+
?: this?.displayName?.takeIf { it.isNotBlank() }
31+
?: this?.uid
32+
?: ""
33+
34+
/**
35+
* Returns the user's email if it is non-blank, otherwise returns the provided [fallback].
36+
*/
37+
fun FirebaseUser?.getDisplayEmail(fallback: String): String =
38+
this?.email?.takeIf { it.isNotBlank() } ?: fallback

0 commit comments

Comments
 (0)