import {Alert, Button, Form, Input, Typography, message} from "antd"; import { LockOutlined, LogoutOutlined } from "@ant-design/icons"; import {useEffect, useMemo, useState} from "react"; import { useNavigate } from "react-router-dom"; import { getCurrentUser, updateMyPassword } from "@/api"; import {fetchPublicPasswordPolicy, type PasswordPolicyPublic} from "@/api/auth"; import {buildPasswordPolicyValidator, buildPolicyHints} from "@/utils/password"; import AuthShell from "../components/AuthShell"; const {Text} = Typography; type ResetPasswordFormValues = { oldPassword: string; newPassword: string; confirmPassword: string; }; const RESET_HIGHLIGHTS = [ "首次登录必须完成密码更新,避免继续使用初始凭证。", "修改成功后保持当前登录态,无需再次验证。", "若不是本人操作,请立即退出并联系管理员。" ]; export default function ResetPassword() { const [loading, setLoading] = useState(false); const [policy, setPolicy] = useState(null); const [username, setUsername] = useState(); const navigate = useNavigate(); const [form] = Form.useForm(); const policyHints = useMemo(() => buildPolicyHints(policy), [policy]); useEffect(() => { const init = async () => { const [profileResult, policyResult] = await Promise.allSettled([getCurrentUser(), fetchPublicPasswordPolicy()]); if (profileResult.status === "fulfilled") { setUsername(profileResult.value.username); } setPolicy(policyResult.status === "fulfilled" ? policyResult.value : null); }; void init(); }, []); const goToLogin = () => { localStorage.removeItem("accessToken"); localStorage.removeItem("refreshToken"); localStorage.removeItem("displayName"); localStorage.removeItem("username"); localStorage.removeItem("availableTenants"); localStorage.removeItem("activeTenantId"); sessionStorage.removeItem("userProfile"); navigate("/login", { replace: true }); }; const onFinish = async (values: ResetPasswordFormValues) => { setLoading(true); try { await updateMyPassword({ oldPassword: values.oldPassword, newPassword: values.newPassword }); const profile = await getCurrentUser(); sessionStorage.setItem("userProfile", JSON.stringify(profile)); window.dispatchEvent(new Event("user-profile-updated")); message.success("密码已更新"); navigate("/", { replace: true }); } finally { setLoading(false); } }; return ( 如需稍后处理,请先退出当前账号。} >
} autoComplete="current-password"/> {policyHints.length > 0 ? (
密码规则
    {policyHints.map((hint) => (
  • {hint}
  • ))}
) : null} username)} ]} > } autoComplete="new-password"/> ({ validator(_, value) { if (!value || getFieldValue("newPassword") === value) { return Promise.resolve(); } return Promise.reject(new Error("两次输入的新密码不一致")); } }) ]} > } autoComplete="new-password"/>
); }