"use client" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle, } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { useEffect, useState } from "react" import { getCaptchaConfig, requestEmailVerifyCode, userRegister } from "@/api/user" import { useRouter, useSearchParams } from "next/navigation" import { useTranslations } from "next-intl" import Captcha from "@/components/common/captcha" import { CaptchaProvider } from "@/models/captcha" import { toast } from "sonner" import { CurrentLogged } from "@/components/auth/common/current-logged" import { SectionDivider } from "@/components/common/section-divider" import { InputOTPControlled } from "@/components/common/input-otp" import { BaseErrorResponse } from "@/models/resp" import { useAuth } from "@/contexts/auth-context" export function RegisterForm({ className, ...props }: React.ComponentProps<"div">) { const { setUser } = useAuth(); const t = useTranslations('Register') const commonT = useTranslations('Common') const router = useRouter() const searchParams = useSearchParams() const redirectBack = searchParams.get("redirect_back") || "/" const [captchaProps, setCaptchaProps] = useState<{ provider: CaptchaProvider siteKey: string url?: string } | null>(null) const [captchaToken, setCaptchaToken] = useState(null) const [isLogging, setIsLogging] = useState(false) const [refreshCaptchaKey, setRefreshCaptchaKey] = useState(0) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [email, setEmail] = useState('') const [verifyCode, setVerifyCode] = useState('') useEffect(() => { getCaptchaConfig() .then((res) => { setCaptchaProps(res.data) }) .catch((error) => { toast.error(t("fetch_captcha_config_failed") + (error?.message ? `: ${error.message}` : "")) setCaptchaProps(null) }) }, [refreshCaptchaKey, t]) const handleCaptchaError = (error: string) => { toast.error(t("captcha_error") + (error ? `: ${error}` : "")); setTimeout(() => { setRefreshCaptchaKey(k => k + 1); }, 1500); } const handleSendVerifyCode = () => { requestEmailVerifyCode(email) .then(() => { toast.success(t("send_verify_code_success")) }) .catch((error: BaseErrorResponse) => { toast.error(`${t("send_verify_code_failed")}: ${error.response.data.message}`) }) } const handleRegister = () => { if (!username || !password || !email) { toast.error(t("please_fill_in_all_required_fields")); return; } if (!captchaToken) { toast.error(t("please_complete_captcha_verification")); return; } userRegister({ username, password, email, verifyCode, captchaToken }) .then(res => { toast.success(t("register_success") + ` ${res.data.user.nickname || res.data.user.username}`); setUser(res.data.user); router.push(redirectBack) }) .catch((error: BaseErrorResponse) => { toast.error(t("register_failed") + (error?.response?.data?.message ? `: ${error.response.data.message}` : "")) setRefreshCaptchaKey(k => k + 1) setCaptchaToken(null) }) .finally(() => { setIsLogging(false) }) } return (
{t("title")}
{t("register_a_new_account")}
{/* 用户名 */}
setUsername(e.target.value)} />
{/* 密码 */}
setPassword(e.target.value)} />
{/* 邮箱 */}
setEmail(e.target.value)} />
{/* 邮箱验证码 */}
setVerifyCode(value)} />
{captchaProps &&
}
) }