import { useState, type FormEvent } from "react"; import { useAuth } from "../auth/AuthContext"; import { useI18n } from "../i18n"; import { apiPost } from "../services/api"; export function ForceChangePasswordPage() { const { t } = useI18n(); const { user, refreshMe, logout } = useAuth(); const [oldPassword, setOldPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirm, setConfirm] = useState(""); const [error, setError] = useState(""); const [busy, setBusy] = useState(false); const onSubmit = async (e: FormEvent) => { e.preventDefault(); setError(""); if (newPassword.length < 6) { setError(t("auth.passwordTooShort")); return; } if (newPassword !== confirm) { setError(t("auth.passwordMismatch")); return; } if (newPassword === oldPassword || newPassword === "admin123") { setError(t("auth.passwordMustChange")); return; } setBusy(true); try { await apiPost("/v1/auth/change-password", { old_password: oldPassword, new_password: newPassword, }); await refreshMe(); } catch (err) { setError(String(err instanceof Error ? err.message : err)); } finally { setBusy(false); } }; return (
void onSubmit(e)}>
NETX

{t("auth.forceChangeTitle")}

{t("auth.forceChangeHint", { user: user?.username || "admin" })}

{error ?
{error}
: null}
); }