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 @@ -42,6 +42,8 @@ import com.firebase.ui.auth.configuration.theme.AuthUITheme
import com.firebase.ui.auth.ui.screens.AuthSuccessUiContext
import com.firebase.ui.auth.ui.screens.FirebaseAuthScreen
import com.firebase.ui.auth.util.EmailLinkConstants
import com.firebase.ui.auth.util.displayIdentifier
import com.firebase.ui.auth.util.getDisplayEmail
import com.google.firebase.auth.actionCodeSettings

class HighLevelApiDemoActivity : ComponentActivity() {
Expand Down Expand Up @@ -211,7 +213,7 @@ private fun AppAuthenticatedContent(
when (state) {
is AuthState.Success -> {
val user = uiContext.authUI.getCurrentUser()
val identifier = user?.email ?: user?.phoneNumber ?: user?.uid.orEmpty()
val identifier = user.displayIdentifier()
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
Expand Down Expand Up @@ -263,7 +265,7 @@ private fun AppAuthenticatedContent(
}

is AuthState.RequiresEmailVerification -> {
val email = uiContext.authUI.getCurrentUser()?.email ?: stringProvider.emailProvider
val email = uiContext.authUI.getCurrentUser().getDisplayEmail(stringProvider.emailProvider)
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ import com.firebase.ui.auth.ui.screens.email.EmailAuthScreen
import com.firebase.ui.auth.ui.screens.phone.PhoneAuthScreen
import com.firebase.ui.auth.util.EmailLinkPersistenceManager
import com.firebase.ui.auth.util.SignInPreferenceManager
import com.firebase.ui.auth.util.displayIdentifier
import com.firebase.ui.auth.util.getDisplayEmail
import com.google.firebase.auth.AuthCredential
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.MultiFactorResolver
Expand Down Expand Up @@ -733,7 +735,7 @@ private fun AuthSuccessContent(
onManageMfa: () -> Unit,
) {
val user = authUI.getCurrentUser()
val userIdentifier = user?.email ?: user?.phoneNumber ?: user?.uid.orEmpty()
val userIdentifier = user.displayIdentifier()
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
Expand Down Expand Up @@ -783,7 +785,7 @@ private fun EmailVerificationContent(
onSignOut: () -> Unit,
) {
val user = authUI.getCurrentUser()
val emailLabel = user?.email ?: stringProvider.emailProvider
val emailLabel = user.getDisplayEmail(stringProvider.emailProvider)
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
Expand Down
38 changes: 38 additions & 0 deletions auth/src/main/java/com/firebase/ui/auth/util/UserUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2025 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.firebase.ui.auth.util

import com.google.firebase.auth.FirebaseUser

/**
* Returns the best available display identifier for the user, trying each field in order:
* email → phoneNumber → displayName → uid.
*
* Each field is checked for blank (not just null) so that an empty string returned by the
* Firebase SDK falls through to the next candidate rather than being displayed as-is.
* Returns an empty string if the user is null.
*/
fun FirebaseUser?.displayIdentifier(): String =
this?.email?.takeIf { it.isNotBlank() }
?: this?.phoneNumber?.takeIf { it.isNotBlank() }
?: this?.displayName?.takeIf { it.isNotBlank() }
?: this?.uid
?: ""

/**
* Returns the user's email if it is non-blank, otherwise returns the provided [fallback].
*/
fun FirebaseUser?.getDisplayEmail(fallback: String): String =
this?.email?.takeIf { it.isNotBlank() } ?: fallback
Loading