182 lines
6.2 KiB
TypeScript
182 lines
6.2 KiB
TypeScript
"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 Link from "next/link"
|
|
import { ThemeToggle } from "@/components/theme-toggle"
|
|
|
|
const formSchema = z.object({
|
|
username: z.string().min(2, {
|
|
message: "用户名至少需要2个字符",
|
|
}),
|
|
password: z.string().min(6, {
|
|
message: "密码至少需要6个字符",
|
|
}),
|
|
email: z.string().email({
|
|
message: "请输入有效的电子邮件地址",
|
|
}),
|
|
phone: z.string().min(11, {
|
|
message: "请输入有效的电话号码",
|
|
}),
|
|
})
|
|
|
|
export default function RegisterPage() {
|
|
const { toast } = useToast()
|
|
const router = useRouter()
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: {
|
|
username: "",
|
|
password: "",
|
|
email: "",
|
|
phone: "",
|
|
},
|
|
})
|
|
|
|
async function onSubmit(values: z.infer<typeof formSchema>) {
|
|
try {
|
|
const response = await fetch("/api/reader/register", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(values),
|
|
})
|
|
|
|
const result = await response.json()
|
|
|
|
if (result.code === 0) {
|
|
toast({
|
|
title: "注册成功",
|
|
description: "您已成功注册,请登录",
|
|
})
|
|
router.push("/login")
|
|
} else {
|
|
toast({
|
|
variant: "destructive",
|
|
title: "注册失败",
|
|
description: result.msg || "注册失败,请稍后再试",
|
|
})
|
|
}
|
|
} catch (error) {
|
|
toast({
|
|
variant: "destructive",
|
|
title: "注册失败",
|
|
description: "服务器连接错误,请稍后再试",
|
|
})
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="container relative flex min-h-screen flex-col items-center justify-center md:grid lg:max-w-none lg:grid-cols-2 lg:px-0">
|
|
<div className="absolute right-4 top-4 md:right-8 md:top-8">
|
|
<ThemeToggle />
|
|
</div>
|
|
<div className="relative hidden h-full flex-col bg-muted p-10 text-white lg:flex dark:border-r">
|
|
<div className="absolute inset-0 bg-zinc-900" />
|
|
<div className="relative z-20 flex items-center text-lg font-medium">
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
strokeWidth="2"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
className="mr-2 h-6 w-6"
|
|
>
|
|
<path d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253" />
|
|
</svg>
|
|
图书管理系统
|
|
</div>
|
|
<div className="relative z-20 mt-auto">
|
|
<blockquote className="space-y-2">
|
|
<p className="text-lg">"读一本好书,就是和许多高尚的人谈话。"</p>
|
|
<footer className="text-sm">笛卡尔</footer>
|
|
</blockquote>
|
|
</div>
|
|
</div>
|
|
<div className="lg:p-8">
|
|
<div className="mx-auto flex w-full flex-col justify-center space-y-6 sm:w-[350px]">
|
|
<div className="flex flex-col space-y-2 text-center">
|
|
<h1 className="text-2xl font-semibold tracking-tight">创建账户</h1>
|
|
<p className="text-sm text-muted-foreground">输入您的信息注册新账户</p>
|
|
</div>
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
|
<FormField
|
|
control={form.control}
|
|
name="username"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>用户名</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder="请输入用户名" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="password"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>密码</FormLabel>
|
|
<FormControl>
|
|
<Input type="password" placeholder="请输入密码" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="email"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>电子邮件</FormLabel>
|
|
<FormControl>
|
|
<Input type="email" placeholder="请输入电子邮件" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="phone"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>电话号码</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder="请输入电话号码" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<Button type="submit" className="w-full">
|
|
注册
|
|
</Button>
|
|
</form>
|
|
</Form>
|
|
<div className="text-center text-sm text-muted-foreground">
|
|
已有账户?{" "}
|
|
<Link href="/login" className="underline underline-offset-4 hover:text-primary">
|
|
登录
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|