feat: Add password dialog for enabling biometric authentication in ProfileScreen
This commit is contained in:
@@ -161,6 +161,9 @@ fun ProfileScreen(
|
|||||||
// Biometric state
|
// Biometric state
|
||||||
var biometricAvailable by remember { mutableStateOf<BiometricAvailability>(BiometricAvailability.NotAvailable("Checking...")) }
|
var biometricAvailable by remember { mutableStateOf<BiometricAvailability>(BiometricAvailability.NotAvailable("Checking...")) }
|
||||||
var isBiometricEnabled by remember { mutableStateOf(false) }
|
var isBiometricEnabled by remember { mutableStateOf(false) }
|
||||||
|
var showPasswordDialog by remember { mutableStateOf(false) }
|
||||||
|
var passwordInput by remember { mutableStateOf("") }
|
||||||
|
var passwordError by remember { mutableStateOf<String?>(null) }
|
||||||
|
|
||||||
// Check biometric availability
|
// Check biometric availability
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
@@ -430,9 +433,9 @@ fun ProfileScreen(
|
|||||||
TelegramBiometricItem(
|
TelegramBiometricItem(
|
||||||
isEnabled = isBiometricEnabled,
|
isEnabled = isBiometricEnabled,
|
||||||
onToggle = {
|
onToggle = {
|
||||||
scope.launch {
|
|
||||||
if (isBiometricEnabled) {
|
if (isBiometricEnabled) {
|
||||||
// Disable biometric
|
// Disable biometric
|
||||||
|
scope.launch {
|
||||||
biometricPrefs.disableBiometric()
|
biometricPrefs.disableBiometric()
|
||||||
biometricPrefs.removeEncryptedPassword(accountPublicKey)
|
biometricPrefs.removeEncryptedPassword(accountPublicKey)
|
||||||
isBiometricEnabled = false
|
isBiometricEnabled = false
|
||||||
@@ -441,16 +444,12 @@ fun ProfileScreen(
|
|||||||
"Biometric authentication disabled",
|
"Biometric authentication disabled",
|
||||||
android.widget.Toast.LENGTH_SHORT
|
android.widget.Toast.LENGTH_SHORT
|
||||||
).show()
|
).show()
|
||||||
} else {
|
|
||||||
// Enable biometric
|
|
||||||
biometricPrefs.enableBiometric()
|
|
||||||
isBiometricEnabled = true
|
|
||||||
android.widget.Toast.makeText(
|
|
||||||
context,
|
|
||||||
"Biometric authentication enabled. Your password will be securely saved on next unlock.",
|
|
||||||
android.widget.Toast.LENGTH_LONG
|
|
||||||
).show()
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Enable biometric - show password dialog first
|
||||||
|
passwordInput = ""
|
||||||
|
passwordError = null
|
||||||
|
showPasswordDialog = true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
isDarkTheme = isDarkTheme
|
isDarkTheme = isDarkTheme
|
||||||
@@ -504,6 +503,100 @@ fun ProfileScreen(
|
|||||||
avatarRepository = avatarRepository
|
avatarRepository = avatarRepository
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Password dialog for biometric setup
|
||||||
|
if (showPasswordDialog && activity != null) {
|
||||||
|
AlertDialog(
|
||||||
|
onDismissRequest = {
|
||||||
|
showPasswordDialog = false
|
||||||
|
passwordInput = ""
|
||||||
|
passwordError = null
|
||||||
|
},
|
||||||
|
title = { Text("Enable Biometric Authentication") },
|
||||||
|
text = {
|
||||||
|
Column {
|
||||||
|
Text("Enter your password to securely save it for biometric unlock:")
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
OutlinedTextField(
|
||||||
|
value = passwordInput,
|
||||||
|
onValueChange = {
|
||||||
|
passwordInput = it
|
||||||
|
passwordError = null
|
||||||
|
},
|
||||||
|
label = { Text("Password") },
|
||||||
|
singleLine = true,
|
||||||
|
visualTransformation = androidx.compose.ui.text.input.PasswordVisualTransformation(),
|
||||||
|
isError = passwordError != null,
|
||||||
|
modifier = Modifier.fillMaxWidth()
|
||||||
|
)
|
||||||
|
if (passwordError != null) {
|
||||||
|
Spacer(modifier = Modifier.height(4.dp))
|
||||||
|
Text(
|
||||||
|
text = passwordError!!,
|
||||||
|
color = Color.Red,
|
||||||
|
fontSize = 12.sp
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
confirmButton = {
|
||||||
|
TextButton(
|
||||||
|
onClick = {
|
||||||
|
if (passwordInput.isEmpty()) {
|
||||||
|
passwordError = "Password cannot be empty"
|
||||||
|
return@TextButton
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to encrypt the password with biometric
|
||||||
|
biometricManager.encryptPassword(
|
||||||
|
activity = activity,
|
||||||
|
password = passwordInput,
|
||||||
|
onSuccess = { encryptedPassword ->
|
||||||
|
scope.launch {
|
||||||
|
// Save encrypted password
|
||||||
|
biometricPrefs.saveEncryptedPassword(accountPublicKey, encryptedPassword)
|
||||||
|
// Enable biometric
|
||||||
|
biometricPrefs.enableBiometric()
|
||||||
|
isBiometricEnabled = true
|
||||||
|
|
||||||
|
showPasswordDialog = false
|
||||||
|
passwordInput = ""
|
||||||
|
passwordError = null
|
||||||
|
|
||||||
|
android.widget.Toast.makeText(
|
||||||
|
context,
|
||||||
|
"Biometric authentication enabled successfully",
|
||||||
|
android.widget.Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onError = { error ->
|
||||||
|
passwordError = error
|
||||||
|
},
|
||||||
|
onCancel = {
|
||||||
|
showPasswordDialog = false
|
||||||
|
passwordInput = ""
|
||||||
|
passwordError = null
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
Text("Enable")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
dismissButton = {
|
||||||
|
TextButton(
|
||||||
|
onClick = {
|
||||||
|
showPasswordDialog = false
|
||||||
|
passwordInput = ""
|
||||||
|
passwordError = null
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
Text("Cancel")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ═════════════════════════════════════════════════════════════
|
// ═════════════════════════════════════════════════════════════
|
||||||
|
|||||||
Reference in New Issue
Block a user