feat(user): require email when admin creates a user

- Add `email` as a required field in `createUserRequest`
- Enforce email format verification and database uniqueness checks in the admin user creation handler
- Update the admin user creation frontend modal with validation and form field
- Update the corresponding backend unit tests and regenerate Swagger docs
This commit is contained in:
ryan
2026-06-13 16:23:07 +08:00
parent 5b13d5b464
commit 922f764241
8 changed files with 115 additions and 0 deletions
@@ -13,6 +13,7 @@ const emptyForm: CreateUserRequest = {
username: "",
password: "",
nickname: "",
email: "",
is_active: true,
is_admin: false,
}
@@ -46,6 +47,12 @@ export function CreateUserModal({
newErrors.username = "用户名长度不能少于 3 位"
}
if (!form.email.trim()) {
newErrors.email = "邮箱不能为空"
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email.trim())) {
newErrors.email = "邮箱格式不正确"
}
if (!form.password) {
newErrors.password = "密码不能为空"
} else if (form.password.length < 8) {
@@ -66,6 +73,7 @@ export function CreateUserModal({
...form,
username: form.username.trim(),
nickname: form.nickname?.trim() || undefined,
email: form.email.trim(),
})
onClose()
} catch {
@@ -109,6 +117,20 @@ export function CreateUserModal({
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="email">邮箱</Label>
<Input
id="email"
type="email"
value={form.email}
onChange={(e) => setForm((prev) => ({ ...prev, email: e.target.value }))}
placeholder="请输入邮箱地址"
/>
{errors.email && (
<p className="text-xs text-destructive">{errors.email}</p>
)}
</div>
<div className="space-y-1.5">
<Label htmlFor="password">密码</Label>
<Input