|
| 1 | +package com.firebaseui.android.demo |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import androidx.activity.ComponentActivity |
| 5 | +import androidx.activity.compose.setContent |
| 6 | +import androidx.activity.enableEdgeToEdge |
| 7 | +import androidx.compose.foundation.layout.Arrangement |
| 8 | +import androidx.compose.foundation.layout.Column |
| 9 | +import androidx.compose.foundation.layout.Spacer |
| 10 | +import androidx.compose.foundation.layout.fillMaxSize |
| 11 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 12 | +import androidx.compose.foundation.layout.height |
| 13 | +import androidx.compose.foundation.layout.padding |
| 14 | +import androidx.compose.material3.Button |
| 15 | +import androidx.compose.material3.Card |
| 16 | +import androidx.compose.material3.CardDefaults |
| 17 | +import androidx.compose.material3.MaterialTheme |
| 18 | +import androidx.compose.material3.OutlinedButton |
| 19 | +import androidx.compose.material3.Surface |
| 20 | +import androidx.compose.material3.Text |
| 21 | +import androidx.compose.runtime.Composable |
| 22 | +import androidx.compose.ui.Alignment |
| 23 | +import androidx.compose.ui.Modifier |
| 24 | +import androidx.compose.ui.text.style.TextAlign |
| 25 | +import androidx.compose.ui.unit.dp |
| 26 | +import com.firebase.ui.auth.AuthException |
| 27 | +import com.firebase.ui.auth.AuthState |
| 28 | +import com.firebase.ui.auth.FirebaseAuthUI |
| 29 | +import com.firebase.ui.auth.configuration.authUIConfiguration |
| 30 | +import com.firebase.ui.auth.configuration.auth_provider.AuthProvider |
| 31 | +import com.firebase.ui.auth.ui.screens.AuthRoute |
| 32 | +import com.firebase.ui.auth.ui.screens.AuthSuccessUiContext |
| 33 | +import com.firebase.ui.auth.ui.screens.FirebaseAuthScreen |
| 34 | + |
| 35 | +class CredentialLinkingDemoActivity : ComponentActivity() { |
| 36 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 37 | + super.onCreate(savedInstanceState) |
| 38 | + enableEdgeToEdge() |
| 39 | + |
| 40 | + val authUI = FirebaseAuthUI.getInstance() |
| 41 | + |
| 42 | + val configuration = authUIConfiguration { |
| 43 | + context = applicationContext |
| 44 | + isCredentialLinkingEnabled = true |
| 45 | + providers { |
| 46 | + provider( |
| 47 | + AuthProvider.Email( |
| 48 | + isNewAccountsAllowed = true, |
| 49 | + emailLinkActionCodeSettings = null, |
| 50 | + passwordValidationRules = emptyList(), |
| 51 | + ) |
| 52 | + ) |
| 53 | + provider( |
| 54 | + AuthProvider.Google( |
| 55 | + scopes = listOf("email"), |
| 56 | + serverClientId = "406099696497-a12gakvts4epfk5pkio7dphc1anjiggc.apps.googleusercontent.com", |
| 57 | + ) |
| 58 | + ) |
| 59 | + provider( |
| 60 | + AuthProvider.Phone( |
| 61 | + defaultNumber = null, |
| 62 | + defaultCountryCode = null, |
| 63 | + allowedCountries = emptyList(), |
| 64 | + timeout = 120L, |
| 65 | + ) |
| 66 | + ) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + setContent { |
| 71 | + MaterialTheme { |
| 72 | + Surface( |
| 73 | + modifier = Modifier.fillMaxSize(), |
| 74 | + color = MaterialTheme.colorScheme.background |
| 75 | + ) { |
| 76 | + FirebaseAuthScreen( |
| 77 | + configuration = configuration, |
| 78 | + authUI = authUI, |
| 79 | + onSignInSuccess = {}, |
| 80 | + onSignInFailure = { _: AuthException -> }, |
| 81 | + onSignInCancelled = {}, |
| 82 | + authenticatedContent = { state, uiContext -> |
| 83 | + CredentialLinkingAuthenticatedContent(state, uiContext) |
| 84 | + } |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +@Composable |
| 93 | +private fun CredentialLinkingAuthenticatedContent( |
| 94 | + state: AuthState, |
| 95 | + uiContext: AuthSuccessUiContext, |
| 96 | +) { |
| 97 | + when (state) { |
| 98 | + is AuthState.Success -> { |
| 99 | + val user = state.user |
| 100 | + Column( |
| 101 | + modifier = Modifier |
| 102 | + .fillMaxSize() |
| 103 | + .padding(24.dp), |
| 104 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 105 | + verticalArrangement = Arrangement.Center |
| 106 | + ) { |
| 107 | + Text( |
| 108 | + text = "Signed in", |
| 109 | + style = MaterialTheme.typography.headlineSmall, |
| 110 | + ) |
| 111 | + Spacer(modifier = Modifier.height(16.dp)) |
| 112 | + Card( |
| 113 | + modifier = Modifier.fillMaxWidth(), |
| 114 | + colors = CardDefaults.cardColors( |
| 115 | + containerColor = MaterialTheme.colorScheme.surfaceVariant |
| 116 | + ) |
| 117 | + ) { |
| 118 | + Column( |
| 119 | + modifier = Modifier.padding(16.dp), |
| 120 | + verticalArrangement = Arrangement.spacedBy(8.dp) |
| 121 | + ) { |
| 122 | + Text("UID: ${user.uid}", style = MaterialTheme.typography.bodySmall) |
| 123 | + Text("Email: ${user.email ?: "—"}") |
| 124 | + Text("Phone: ${user.phoneNumber ?: "—"}") |
| 125 | + Text( |
| 126 | + "Providers: ${user.providerData.map { it.providerId }}", |
| 127 | + style = MaterialTheme.typography.bodySmall, |
| 128 | + textAlign = TextAlign.Start |
| 129 | + ) |
| 130 | + } |
| 131 | + } |
| 132 | + Spacer(modifier = Modifier.height(24.dp)) |
| 133 | + Button( |
| 134 | + modifier = Modifier.fillMaxWidth(), |
| 135 | + onClick = { uiContext.onNavigate(AuthRoute.MethodPicker) } |
| 136 | + ) { |
| 137 | + Text("Add sign-in method") |
| 138 | + } |
| 139 | + Spacer(modifier = Modifier.height(8.dp)) |
| 140 | + OutlinedButton( |
| 141 | + modifier = Modifier.fillMaxWidth(), |
| 142 | + onClick = uiContext.onSignOut |
| 143 | + ) { |
| 144 | + Text("Sign out") |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + is AuthState.RequiresEmailVerification -> { |
| 150 | + Column( |
| 151 | + modifier = Modifier |
| 152 | + .fillMaxSize() |
| 153 | + .padding(24.dp), |
| 154 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 155 | + verticalArrangement = Arrangement.Center |
| 156 | + ) { |
| 157 | + Text( |
| 158 | + text = "Verify your email", |
| 159 | + style = MaterialTheme.typography.headlineSmall, |
| 160 | + ) |
| 161 | + Spacer(modifier = Modifier.height(8.dp)) |
| 162 | + Text( |
| 163 | + text = "A verification link was sent to ${state.email}. Once verified, tap the button below.", |
| 164 | + textAlign = TextAlign.Center, |
| 165 | + color = MaterialTheme.colorScheme.onSurfaceVariant |
| 166 | + ) |
| 167 | + Spacer(modifier = Modifier.height(24.dp)) |
| 168 | + Button( |
| 169 | + modifier = Modifier.fillMaxWidth(), |
| 170 | + onClick = uiContext.onReloadUser |
| 171 | + ) { |
| 172 | + Text("I've verified my email") |
| 173 | + } |
| 174 | + Spacer(modifier = Modifier.height(8.dp)) |
| 175 | + OutlinedButton( |
| 176 | + modifier = Modifier.fillMaxWidth(), |
| 177 | + onClick = uiContext.onSignOut |
| 178 | + ) { |
| 179 | + Text("Sign out") |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + else -> {} |
| 185 | + } |
| 186 | +} |
0 commit comments