import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { useNavigate, useSearchParams } from "react-router-dom"; import { fetchSsoProviders } from "../api/auth"; import { mapApiErrorMessage } from "../api/errorMessages"; import { isApiError } from "../api/types"; import { useAuth } from "../auth/AuthContext"; import { Button } from "../components/ui/button"; import { Input } from "../components/ui/input"; import { Label } from "../components/ui/label"; function resolveRedirect(searchParams: URLSearchParams) { const redirect = searchParams.get("redirect"); if (!redirect) { return "/portal"; } if (!redirect.startsWith("/")) { return "/portal"; } return redirect; } export function LoginPage() { const navigate = useNavigate(); const [searchParams] = useSearchParams(); const { login } = useAuth(); const [identifier, setIdentifier] = useState("employee@hyinda.com"); const [password, setPassword] = useState("EmployeePassword1234"); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(""); const redirectTo = resolveRedirect(searchParams); const ssoProvidersQuery = useQuery({ queryKey: ["auth", "sso-providers"], queryFn: () => fetchSsoProviders(), staleTime: 60_000, }); const oidcProvider = (ssoProvidersQuery.data ?? []).find((item) => item.id === "oidc" && item.enabled); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); setSubmitting(true); setError(""); try { const result = await login(identifier.trim(), password); if (result.mustChangePassword) { navigate("/change-password", { replace: true }); } else { navigate(redirectTo, { replace: true }); } } catch (err) { if (isApiError(err)) { setError(`${err.code}: ${mapApiErrorMessage(err.code, err.details, err.message)}`); } else if (err instanceof TypeError) { setError("无法连接后端服务,请确认 server 已启动(http://127.0.0.1:3000)。"); } else { setError("登录失败,请稍后重试"); } } finally { setSubmitting(false); } } return (

hyb-oa

登录工作台

使用邮箱/手机号与密码登录。

忘记密码:请联系管理员重置。

setIdentifier(event.target.value)} placeholder="邮箱或手机号" value={identifier} />
setPassword(event.target.value)} placeholder="请输入密码" type="password" value={password} />
{error ? (

{error}

) : null}
{oidcProvider ? (

企业统一入口

) : null}
); }