"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" const schema = z.object({ username: z.string().min(1, "用户名不能为空"), password: z.string().min(1, "密码不能为空"), }) export function LoginForm({ className, ...props }: React.ComponentProps<"form">) { const { login } = useUser(); const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const form = useForm>({ resolver: zodResolver(schema), defaultValues: { username: "", password: "", }, }) async function onSubmit(values: z.infer) { try { setIsLoading(true); setError(null); await login(values); // clearMap(); router.push('/'); } catch (err) { setError(err instanceof Error ? err.message : '登录失败,请重试'); } finally { setIsLoading(false); } } return (

登录您的账户

请输入您的用户名和密码登录

{error && (
{error}
)}
( 用户名 )} />
( )} />
或者使用
还没有账户?{" "} 注册
) }