RegisterPage.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { useForm } from 'react-hook-form';
  2. import { zodResolver } from '@hookform/resolvers/zod';
  3. import { z } from 'zod';
  4. import { Link, useNavigate } from 'react-router-dom';
  5. import { useAuth } from '@/client/home/hooks/AuthProvider';
  6. import { toast } from 'sonner';
  7. import { authClient } from '@/client/api';
  8. import { Button } from '@/client/components/ui/button';
  9. import { Input } from '@/client/components/ui/input';
  10. import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/client/components/ui/card';
  11. import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/client/components/ui/form';
  12. import { User, Lock } from 'lucide-react';
  13. const registerSchema = z.object({
  14. username: z.string()
  15. .min(3, '用户名至少3个字符')
  16. .max(20, '用户名不能超过20个字符')
  17. .regex(/^[a-zA-Z0-9_-]+$/, '用户名只能包含字母、数字、下划线和连字符'),
  18. password: z.string()
  19. .min(6, '密码至少6个字符')
  20. .max(30, '密码不能超过30个字符'),
  21. confirmPassword: z.string(),
  22. }).refine((data) => data.password === data.confirmPassword, {
  23. message: '两次密码输入不一致',
  24. path: ['confirmPassword'],
  25. });
  26. type RegisterFormData = z.infer<typeof registerSchema>;
  27. const RegisterPage: React.FC = () => {
  28. const { login } = useAuth();
  29. const navigate = useNavigate();
  30. const form = useForm<RegisterFormData>({
  31. resolver: zodResolver(registerSchema),
  32. defaultValues: {
  33. username: '',
  34. password: '',
  35. confirmPassword: '',
  36. },
  37. });
  38. const onSubmit = async (data: RegisterFormData) => {
  39. try {
  40. const response = await authClient.register.$post({
  41. json: {
  42. username: data.username,
  43. password: data.password,
  44. }
  45. });
  46. if (response.status !== 201) {
  47. const result = await response.json();
  48. throw new Error(result.message || '注册失败');
  49. }
  50. // 注册成功后自动登录
  51. await login(data.username, data.password);
  52. toast.success('注册成功!正在为您登录...');
  53. navigate('/');
  54. } catch (error) {
  55. toast.error(error instanceof Error ? error.message : '注册失败,请稍后重试');
  56. }
  57. };
  58. return (
  59. <div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-slate-50 to-slate-100 px-4 py-12">
  60. <Card className="w-full max-w-md border-0 shadow-xl">
  61. <CardHeader className="space-y-1">
  62. <CardTitle className="text-2xl font-bold text-center">创建账号</CardTitle>
  63. <CardDescription className="text-center">
  64. 填写以下信息创建新账号
  65. </CardDescription>
  66. </CardHeader>
  67. <CardContent>
  68. <Form {...form}>
  69. <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
  70. <FormField
  71. control={form.control}
  72. name="username"
  73. render={({ field }) => (
  74. <FormItem>
  75. <FormLabel>用户名</FormLabel>
  76. <FormControl>
  77. <div className="relative">
  78. <User className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
  79. <Input
  80. placeholder="请输入用户名"
  81. className="pl-10"
  82. {...field}
  83. />
  84. </div>
  85. </FormControl>
  86. <FormMessage />
  87. </FormItem>
  88. )}
  89. />
  90. <FormField
  91. control={form.control}
  92. name="password"
  93. render={({ field }) => (
  94. <FormItem>
  95. <FormLabel>密码</FormLabel>
  96. <FormControl>
  97. <div className="relative">
  98. <Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
  99. <Input
  100. type="password"
  101. placeholder="请输入密码"
  102. className="pl-10"
  103. {...field}
  104. />
  105. </div>
  106. </FormControl>
  107. <FormMessage />
  108. </FormItem>
  109. )}
  110. />
  111. <FormField
  112. control={form.control}
  113. name="confirmPassword"
  114. render={({ field }) => (
  115. <FormItem>
  116. <FormLabel>确认密码</FormLabel>
  117. <FormControl>
  118. <div className="relative">
  119. <Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
  120. <Input
  121. type="password"
  122. placeholder="请再次输入密码"
  123. className="pl-10"
  124. {...field}
  125. />
  126. </div>
  127. </FormControl>
  128. <FormMessage />
  129. </FormItem>
  130. )}
  131. />
  132. <Button
  133. type="submit"
  134. className="w-full"
  135. disabled={form.formState.isSubmitting}
  136. >
  137. {form.formState.isSubmitting ? '注册中...' : '注册账号'}
  138. </Button>
  139. </form>
  140. </Form>
  141. </CardContent>
  142. <CardFooter className="flex flex-col space-y-4">
  143. <div className="text-sm text-center">
  144. <span className="text-muted-foreground">已有账号?</span>
  145. <Button
  146. variant="link"
  147. className="px-1"
  148. asChild
  149. >
  150. <Link to="/login">立即登录</Link>
  151. </Button>
  152. </div>
  153. <div className="text-xs text-center text-muted-foreground">
  154. <p>注册即表示您同意我们的服务条款</p>
  155. </div>
  156. </CardFooter>
  157. </Card>
  158. </div>
  159. );
  160. };
  161. export default RegisterPage;