"use client" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import * as z from "zod" import { Button } from "@/components/ui/button" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { useToast } from "@/components/ui/use-toast" import { useRouter } from "next/navigation" import { useAuth } from "@/context/auth-context" import Link from "next/link" import { ThemeToggle } from "@/components/theme-toggle" import { Checkbox } from "@/components/ui/checkbox" import { useState } from "react" const formSchema = z.object({ username: z.string().min(2, { message: "用户名至少需要2个字符", }), password: z.string().min(6, { message: "密码至少需要6个字符", }), rememberMe: z.boolean().default(false), }) export default function LoginPage() { const { toast } = useToast() const router = useRouter() const { login } = useAuth() const [isLoading, setIsLoading] = useState(false) const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { username: "", password: "", rememberMe: false, }, }) async function onSubmit(values: z.infer) { setIsLoading(true) try { const response = await fetch("/api/reader/login", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(values), }) // 从响应头中获取 Authorization token const authToken = response.headers.get("Authorization") if (!authToken) { toast({ variant: "destructive", title: "登录失败", description: "未能获取授权令牌", }) setIsLoading(false) return } const result = await response.json() if (result.code === 0) { login(result.data, authToken) toast({ title: "登录成功", description: "欢迎回来," + result.data.username, }) router.push("/") } else { toast({ variant: "destructive", title: "登录失败", description: result.msg || "用户名或密码错误", }) } } catch (error) { toast({ variant: "destructive", title: "登录失败", description: "服务器连接错误,请稍后再试", }) } finally { setIsLoading(false) } } return (
图书管理系统

"书籍是人类进步的阶梯。"

高尔基

账户登录

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

( 用户名 )} /> ( 密码 )} /> (
记住我
)} />
还没有账户?{" "} 注册
) }