fix(android): new passwords follow the web's rules and wording (DRY B5)
Settings › Password said "New passwords do not match." where the web says "Passwords do not match.", and neither it nor an admin resetting someone's password checked the 8-character minimum before sending: the reset button lit up after one character. auth/PasswordRules mirrors web/src/lib/auth/password.ts: the minimum, the "At least 8 characters." hint under the field, the too-short and mismatch wording. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -31,6 +31,7 @@ import androidx.compose.material3.TextButton
|
|||||||
import android.content.ClipData
|
import android.content.ClipData
|
||||||
import androidx.compose.ui.platform.ClipEntry
|
import androidx.compose.ui.platform.ClipEntry
|
||||||
import androidx.compose.ui.platform.LocalClipboard
|
import androidx.compose.ui.platform.LocalClipboard
|
||||||
|
import com.fabledsword.minstrel.auth.PasswordRules
|
||||||
import com.fabledsword.minstrel.models.Invite
|
import com.fabledsword.minstrel.models.Invite
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
@@ -471,6 +472,7 @@ private fun ResetPasswordDialog(
|
|||||||
value = newPassword,
|
value = newPassword,
|
||||||
onValueChange = { newPassword = it },
|
onValueChange = { newPassword = it },
|
||||||
label = { Text("New password") },
|
label = { Text("New password") },
|
||||||
|
supportingText = { Text(PasswordRules.HINT) },
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
visualTransformation = PasswordVisualTransformation(),
|
visualTransformation = PasswordVisualTransformation(),
|
||||||
)
|
)
|
||||||
@@ -479,7 +481,7 @@ private fun ResetPasswordDialog(
|
|||||||
confirmButton = {
|
confirmButton = {
|
||||||
TextButton(
|
TextButton(
|
||||||
onClick = { onConfirm(newPassword) },
|
onClick = { onConfirm(newPassword) },
|
||||||
enabled = newPassword.isNotEmpty(),
|
enabled = PasswordRules.isLongEnough(newPassword),
|
||||||
) { Text("Reset") }
|
) { Text("Reset") }
|
||||||
},
|
},
|
||||||
dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } },
|
dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } },
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.fabledsword.minstrel.auth
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The rules every new-password field shares (Settings › Password, an admin
|
||||||
|
* resetting someone's password), worded as the web app words them
|
||||||
|
* (web/src/lib/auth/password.ts). The server enforces the same minimum and
|
||||||
|
* answers password_too_short; checking here only saves the round trip.
|
||||||
|
*/
|
||||||
|
object PasswordRules {
|
||||||
|
const val MIN_LENGTH = 8
|
||||||
|
|
||||||
|
/** The hint under a new-password field. */
|
||||||
|
const val HINT = "At least $MIN_LENGTH characters."
|
||||||
|
|
||||||
|
/** Said when a new password is shorter than [MIN_LENGTH]. */
|
||||||
|
const val TOO_SHORT = "Password must be at least $MIN_LENGTH characters."
|
||||||
|
|
||||||
|
/** Said when a password and its confirmation differ. */
|
||||||
|
const val DIFFER = "Passwords do not match."
|
||||||
|
|
||||||
|
fun isLongEnough(password: String): Boolean = password.length >= MIN_LENGTH
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import com.fabledsword.minstrel.auth.PasswordRules
|
||||||
import com.fabledsword.minstrel.theme.Muted
|
import com.fabledsword.minstrel.theme.Muted
|
||||||
import com.fabledsword.minstrel.theme.Section
|
import com.fabledsword.minstrel.theme.Section
|
||||||
import com.fabledsword.minstrel.theme.SwitchRow
|
import com.fabledsword.minstrel.theme.SwitchRow
|
||||||
@@ -36,6 +37,7 @@ fun PasswordCard(viewModel: PasswordViewModel = hiltViewModel()) {
|
|||||||
value = state.next,
|
value = state.next,
|
||||||
label = "New password",
|
label = "New password",
|
||||||
onChange = viewModel::setNext,
|
onChange = viewModel::setNext,
|
||||||
|
hint = PasswordRules.HINT,
|
||||||
)
|
)
|
||||||
PasswordField(
|
PasswordField(
|
||||||
value = state.confirm,
|
value = state.confirm,
|
||||||
@@ -73,11 +75,13 @@ private fun PasswordField(
|
|||||||
value: String,
|
value: String,
|
||||||
label: String,
|
label: String,
|
||||||
onChange: (String) -> Unit,
|
onChange: (String) -> Unit,
|
||||||
|
hint: String? = null,
|
||||||
) {
|
) {
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
value = value,
|
value = value,
|
||||||
onValueChange = onChange,
|
onValueChange = onChange,
|
||||||
label = { Text(label) },
|
label = { Text(label) },
|
||||||
|
supportingText = hint?.let { { Text(it) } },
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
visualTransformation = PasswordVisualTransformation(),
|
visualTransformation = PasswordVisualTransformation(),
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.fabledsword.minstrel.settings.ui
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.fabledsword.minstrel.api.ErrorCopy
|
import com.fabledsword.minstrel.api.ErrorCopy
|
||||||
|
import com.fabledsword.minstrel.auth.PasswordRules
|
||||||
import com.fabledsword.minstrel.settings.data.MeRepository
|
import com.fabledsword.minstrel.settings.data.MeRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
@@ -46,7 +47,8 @@ class PasswordViewModel @Inject constructor(
|
|||||||
val validationError: String? = when {
|
val validationError: String? = when {
|
||||||
s.isChanging -> "" // sentinel: silent no-op
|
s.isChanging -> "" // sentinel: silent no-op
|
||||||
s.current.isEmpty() || s.next.isEmpty() -> "All fields required."
|
s.current.isEmpty() || s.next.isEmpty() -> "All fields required."
|
||||||
s.next != s.confirm -> "New passwords do not match."
|
!PasswordRules.isLongEnough(s.next) -> PasswordRules.TOO_SHORT
|
||||||
|
s.next != s.confirm -> PasswordRules.DIFFER
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
if (validationError != null) {
|
if (validationError != null) {
|
||||||
|
|||||||
+42
@@ -0,0 +1,42 @@
|
|||||||
|
package com.fabledsword.minstrel.settings.ui
|
||||||
|
|
||||||
|
import com.fabledsword.minstrel.auth.PasswordRules
|
||||||
|
import com.fabledsword.minstrel.settings.data.MeRepository
|
||||||
|
import com.fabledsword.minstrel.testutil.MainDispatcherExtension
|
||||||
|
import io.mockk.coVerify
|
||||||
|
import io.mockk.mockk
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A new password is checked as the web app checks it, with the same words,
|
||||||
|
* before anything is sent: at least 8 characters, and the same twice.
|
||||||
|
*/
|
||||||
|
@ExtendWith(MainDispatcherExtension::class)
|
||||||
|
class PasswordViewModelTest {
|
||||||
|
private val repository: MeRepository = mockk(relaxed = true)
|
||||||
|
|
||||||
|
private fun attempt(next: String, confirm: String): PasswordViewModel =
|
||||||
|
PasswordViewModel(repository).apply {
|
||||||
|
setCurrent("old-password")
|
||||||
|
setNext(next)
|
||||||
|
setConfirm(confirm)
|
||||||
|
change()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `a password shorter than 8 characters is refused without asking the server`() {
|
||||||
|
val viewModel = attempt(next = "short", confirm = "short")
|
||||||
|
assertEquals("Password must be at least 8 characters.", viewModel.state.value.message)
|
||||||
|
coVerify(exactly = 0) { repository.changePassword(any(), any(), any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `a confirmation that differs says so as the web does`() {
|
||||||
|
val viewModel = attempt(next = "long-enough-1", confirm = "long-enough-2")
|
||||||
|
assertEquals(PasswordRules.DIFFER, viewModel.state.value.message)
|
||||||
|
assertEquals("Passwords do not match.", PasswordRules.DIFFER)
|
||||||
|
coVerify(exactly = 0) { repository.changePassword(any(), any(), any()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user