"use client" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { z } from "zod" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { useUser } from "../user-context" import { useRouter } from "next/navigation" import { useState } from "react" import { GalleryVerticalEnd } from "lucide-react" const schema = z.object({ username: z.string().min(1, "用户名不能为空"), password: z.string().min(1, "密码不能为空"), }) export function LoginForm({ className, ...props }: React.ComponentProps<"div">) { const { login } = useUser(); const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [step, setStep] = useState<'username' | 'password'>('username'); const [username, setUsername] = useState(''); const usernameForm = useForm<{ username: string }>({ resolver: zodResolver(z.object({ username: z.string().min(1, "用户名不能为空") })), defaultValues: { username: "" }, }); const passwordForm = useForm<{ username: string; password: string }>({ resolver: zodResolver(z.object({ username: z.string(), password: z.string().min(1, "密码不能为空") })), defaultValues: { username: "", password: "" }, }); async function onUsernameSubmit(values: { username: string }) { setUsername(values.username); setStep('password'); setError(null); // 设置密码表单的用户名字段 passwordForm.setValue('username', values.username); } async function onPasswordSubmit(values: { username: string; password: string }) { try { setIsLoading(true); setError(null); await login({ username, password: values.password }); router.push('/'); } catch (err) { setError(err instanceof Error ? err.message : '登录失败,请重试'); } finally { setIsLoading(false); } } function goBackToUsername() { setStep('username'); setError(null); passwordForm.reset(); } return (
{step === 'username' ? (
Acme Inc.

欢迎来到 Acme Inc.

还没有账户?{" "} 注册
{error && (
{error}
)}
( 用户名 )} />
) : (
Acme Inc.

输入密码

登录为 {username}
{error && (
{error}
)}
( 用户名 )} />
( 密码 )} />
)}
By clicking continue, you agree to our{" "} Terms of Service {" "} and{" "} Privacy Policy .
) }