LoginPage.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 { Button } from '@/client/components/ui/button';
  8. import { Input } from '@/client/components/ui/input';
  9. import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/client/components/ui/card';
  10. import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/client/components/ui/form';
  11. import { User, Lock } from 'lucide-react';
  12. const loginSchema = z.object({
  13. username: z.string().min(3, '用户名至少3个字符'),
  14. password: z.string().min(6, '密码至少6个字符'),
  15. });
  16. type LoginFormData = z.infer<typeof loginSchema>;
  17. const LoginPage: React.FC = () => {
  18. const { login } = useAuth();
  19. const navigate = useNavigate();
  20. const form = useForm<LoginFormData>({
  21. resolver: zodResolver(loginSchema),
  22. defaultValues: {
  23. username: '',
  24. password: '',
  25. },
  26. });
  27. const onSubmit = async (data: LoginFormData) => {
  28. try {
  29. await login(data.username, data.password);
  30. toast.success('登录成功!');
  31. navigate('/');
  32. } catch (error) {
  33. toast.error(error instanceof Error ? error.message : '登录失败,请检查用户名和密码');
  34. }
  35. };
  36. return (
  37. <div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-slate-50 to-slate-100 px-4 py-12">
  38. <Card className="w-full max-w-md border-0 shadow-xl">
  39. <CardHeader className="space-y-1">
  40. <CardTitle className="text-2xl font-bold text-center">欢迎回来</CardTitle>
  41. <CardDescription className="text-center">
  42. 登录您的账号以继续
  43. </CardDescription>
  44. </CardHeader>
  45. <CardContent>
  46. <Form {...form}>
  47. <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
  48. <FormField
  49. control={form.control}
  50. name="username"
  51. render={({ field }) => (
  52. <FormItem>
  53. <FormLabel>用户名</FormLabel>
  54. <FormControl>
  55. <div className="relative">
  56. <User className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
  57. <Input
  58. placeholder="请输入用户名"
  59. className="pl-10"
  60. {...field}
  61. />
  62. </div>
  63. </FormControl>
  64. <FormMessage />
  65. </FormItem>
  66. )}
  67. />
  68. <FormField
  69. control={form.control}
  70. name="password"
  71. render={({ field }) => (
  72. <FormItem>
  73. <FormLabel>密码</FormLabel>
  74. <FormControl>
  75. <div className="relative">
  76. <Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
  77. <Input
  78. type="password"
  79. placeholder="请输入密码"
  80. className="pl-10"
  81. {...field}
  82. />
  83. </div>
  84. </FormControl>
  85. <FormMessage />
  86. </FormItem>
  87. )}
  88. />
  89. <Button
  90. type="submit"
  91. className="w-full"
  92. disabled={form.formState.isSubmitting}
  93. >
  94. {form.formState.isSubmitting ? '登录中...' : '登录'}
  95. </Button>
  96. </form>
  97. </Form>
  98. </CardContent>
  99. <CardFooter className="flex flex-col space-y-4">
  100. <div className="text-sm text-center">
  101. <span className="text-muted-foreground">还没有账号?</span>
  102. <Button
  103. variant="link"
  104. className="px-1"
  105. asChild
  106. >
  107. <Link to="/register">立即注册</Link>
  108. </Button>
  109. </div>
  110. <div className="text-xs text-center text-muted-foreground">
  111. <p>测试账号:admin / admin123</p>
  112. </div>
  113. </CardFooter>
  114. </Card>
  115. </div>
  116. );
  117. };
  118. export default LoginPage;