113 行
3.8 KiB
TypeScript
113 行
3.8 KiB
TypeScript
import { resolveErrorMessage as resolveUserErrorMessage } from "../lib/resolveErrorMessage";
|
|
import { useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { isApiError } from "../api/types";
|
|
import { useAuth } from "../auth/AuthContext";
|
|
import { AuthPanel } from "../components/auth/AuthPanel";
|
|
import { Button } from "../components/ui/button";
|
|
import { Label } from "../components/ui/label";
|
|
import { PasswordInput } from "../components/ui/password-input";
|
|
import { markOnboardingEntryReady } from "../lib/onboardingGuide";
|
|
|
|
const PASSWORD_REQUIREMENTS = [
|
|
"至少 12 位",
|
|
"包含大写字母、小写字母和数字",
|
|
"不要使用公司名、常见顺序或过于简单的组合",
|
|
];
|
|
|
|
export function ChangePasswordPage() {
|
|
const navigate = useNavigate();
|
|
const { changePassword } = useAuth();
|
|
|
|
const [oldPassword, setOldPassword] = useState("");
|
|
const [newPassword, setNewPassword] = useState("");
|
|
const [confirmPassword, setConfirmPassword] = useState("");
|
|
const [submitting, setSubmitting] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
setError("");
|
|
|
|
if (newPassword !== confirmPassword) {
|
|
setError("两次输入的新密码不一致,请确认后再提交。");
|
|
return;
|
|
}
|
|
|
|
setSubmitting(true);
|
|
try {
|
|
await changePassword(oldPassword, newPassword);
|
|
markOnboardingEntryReady();
|
|
navigate("/portal", { replace: true });
|
|
} catch (err) {
|
|
if (isApiError(err)) {
|
|
setError(resolveUserErrorMessage(err));
|
|
} else {
|
|
setError("修改密码失败,请稍后重试");
|
|
}
|
|
} finally {
|
|
setSubmitting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<AuthPanel title="修改密码" subtitle="为了账号安全,请先修改初始密码">
|
|
<form className="space-y-4" onSubmit={handleSubmit}>
|
|
<div className="space-y-2">
|
|
<Label htmlFor="oldPassword">旧密码</Label>
|
|
<PasswordInput
|
|
autoComplete="current-password"
|
|
data-testid="change-password-old"
|
|
id="oldPassword"
|
|
onChange={(event) => setOldPassword(event.target.value)}
|
|
placeholder="请输入旧密码"
|
|
value={oldPassword}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="newPassword">新密码</Label>
|
|
<PasswordInput
|
|
autoComplete="new-password"
|
|
data-testid="change-password-new"
|
|
id="newPassword"
|
|
onChange={(event) => setNewPassword(event.target.value)}
|
|
placeholder="至少 12 位,建议包含大小写/数字"
|
|
value={newPassword}
|
|
/>
|
|
<p className="text-xs text-zinc-500">密码要求:{PASSWORD_REQUIREMENTS.join(";")}。</p>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="confirmPassword">确认新密码</Label>
|
|
<PasswordInput
|
|
autoComplete="new-password"
|
|
id="confirmPassword"
|
|
onChange={(event) => setConfirmPassword(event.target.value)}
|
|
placeholder="再次输入新密码"
|
|
value={confirmPassword}
|
|
/>
|
|
</div>
|
|
|
|
{error ? (
|
|
<p
|
|
className="rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-600"
|
|
data-testid="change-password-error"
|
|
>
|
|
{error}
|
|
</p>
|
|
) : null}
|
|
|
|
<Button
|
|
data-testid="change-password-submit"
|
|
className="w-full"
|
|
disabled={submitting || !oldPassword.trim() || !newPassword.trim() || !confirmPassword.trim()}
|
|
type="submit"
|
|
>
|
|
{submitting ? "提交中..." : "确认修改"}
|
|
</Button>
|
|
</form>
|
|
</AuthPanel>
|
|
);
|
|
}
|