import React, { useState } from 'react'; import { useForm } from 'react-hook-form'; import { EyeIcon, EyeSlashIcon, UserIcon, LockClosedIcon } from '@heroicons/react/24/outline'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '@/client/home/hooks/AuthProvider'; import { authClient } from '@/client/api'; const RegisterPage: React.FC = () => { const { register, handleSubmit, watch, formState: { errors } } = useForm(); const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [loading, setLoading] = useState(false); const { login } = useAuth(); const navigate = useNavigate(); const password = watch('password', ''); const onSubmit = async (data: any) => { try { setLoading(true); // 调用注册API const response = await authClient.register.$post({ json: { username: data.username, password: data.password, } }); if (response.status !== 201) { const result = await response.json(); throw new Error(result.message || '注册失败'); } // 注册成功后自动登录 await login(data.username, data.password); // 跳转到首页 navigate('/'); } catch (error) { console.error('Registration error:', error); alert((error as Error).message || '注册失败,请稍后重试'); } finally { setLoading(false); } }; return (
创建新账号以开始使用