added zod for signup validation
This commit is contained in:
@ -10,14 +10,44 @@ import {
|
|||||||
CardHeader,
|
CardHeader,
|
||||||
CardTitle,
|
CardTitle,
|
||||||
} from "@/components/ui/card";
|
} from "@/components/ui/card";
|
||||||
|
import * as z from "zod";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
import { toast } from "@/components/ui/use-toast";
|
import { toast } from "@/components/ui/use-toast";
|
||||||
|
|
||||||
import { signUp } from "./action";
|
import { signUp } from "./action";
|
||||||
|
import { useForm, SubmitHandler, FieldValues } from "react-hook-form";
|
||||||
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
|
|
||||||
|
const signUpSchema = z
|
||||||
|
.object({
|
||||||
|
username: z.string().min(1, "Username is required"),
|
||||||
|
email: z.string().email("Invalid email address"),
|
||||||
|
accessCode: z
|
||||||
|
.string()
|
||||||
|
.regex(/^\d{20}$/, "Access Code must be exactly 20 numeric characters"),
|
||||||
|
password: z.string().min(8, "Password must be at least 8 characters"),
|
||||||
|
confirmPassword: z.string().min(8, "Please confirm your password"),
|
||||||
|
})
|
||||||
|
.refine((data) => data.password === data.confirmPassword, {
|
||||||
|
path: ["confirmPassword"],
|
||||||
|
message: "Passwords do not match",
|
||||||
|
});
|
||||||
|
|
||||||
export default function SignUpForm() {
|
export default function SignUpForm() {
|
||||||
const submit = async (data: FormData) => {
|
const {
|
||||||
const { error } = await signUp(data);
|
register,
|
||||||
|
handleSubmit,
|
||||||
|
formState: { errors },
|
||||||
|
} = useForm({
|
||||||
|
resolver: zodResolver(signUpSchema),
|
||||||
|
});
|
||||||
|
const onSubmit: SubmitHandler<FieldValues> = async (data) => {
|
||||||
|
// Convert FieldValues to FormData
|
||||||
|
const formData = new FormData();
|
||||||
|
for (const [key, value] of Object.entries(data)) {
|
||||||
|
formData.append(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { error } = await signUp(formData);
|
||||||
if (error) {
|
if (error) {
|
||||||
toast({
|
toast({
|
||||||
title: "Error",
|
title: "Error",
|
||||||
@ -40,55 +70,75 @@ export default function SignUpForm() {
|
|||||||
</CardDescription>
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<form action={submit} className="grid gap-4">
|
<form onSubmit={handleSubmit(onSubmit)} className="grid gap-4">
|
||||||
<div className="grid gap-2">
|
<div className="grid gap-2">
|
||||||
<Label htmlFor="username">Username</Label>
|
<Label htmlFor="username">Username</Label>
|
||||||
<Input
|
<Input
|
||||||
name="username"
|
{...register("username")}
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="username"
|
placeholder="username"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
{errors.username?.message &&
|
||||||
|
typeof errors.username.message === "string" && (
|
||||||
|
<p className="text-red-600">{errors.username.message}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid gap-2">
|
<div className="grid gap-2">
|
||||||
<Label htmlFor="email">Email</Label>
|
<Label htmlFor="email">Email</Label>
|
||||||
<Input
|
<Input
|
||||||
name="email"
|
{...register("email")}
|
||||||
type="email"
|
type="email"
|
||||||
placeholder="mail@mail.com"
|
placeholder="mail@mail.com"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
{errors.email?.message &&
|
||||||
|
typeof errors.email.message === "string" && (
|
||||||
|
<p className="text-red-600">{errors.email.message}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid gap-2">
|
<div className="grid gap-2">
|
||||||
<Label htmlFor="accessCode">Access Code</Label>
|
<Label htmlFor="accessCode">Access Code</Label>
|
||||||
<Input
|
<Input
|
||||||
name="accessCode"
|
{...register("accessCode")}
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="*******************"
|
placeholder="*******************"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
{errors.accessCode?.message &&
|
||||||
|
typeof errors.accessCode.message === "string" && (
|
||||||
|
<p className="text-red-600">{errors.accessCode.message}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid gap-2">
|
<div className="grid gap-2">
|
||||||
<Label htmlFor="password">Password</Label>
|
<Label htmlFor="password">Password</Label>
|
||||||
<Input
|
<Input
|
||||||
name="password"
|
{...register("password")}
|
||||||
type="password"
|
type="password"
|
||||||
placeholder="********"
|
placeholder="********"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
{errors.password?.message &&
|
||||||
|
typeof errors.password.message === "string" && (
|
||||||
|
<p className="text-red-600">{errors.password.message}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid gap-2">
|
<div className="grid gap-2">
|
||||||
<Label htmlFor="confirmPassword">Confirm Password</Label>
|
<Label htmlFor="confirmPassword">Confirm Password</Label>
|
||||||
<Input
|
<Input
|
||||||
name="confirmPassword"
|
{...register("confirmPassword")}
|
||||||
type="password"
|
type="password"
|
||||||
placeholder="********"
|
placeholder="********"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
{errors.confirmPassword?.message &&
|
||||||
|
typeof errors.confirmPassword.message === "string" && (
|
||||||
|
<p className="text-red-600">{errors.confirmPassword.message}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button type="submit" className="w-full">
|
<Button type="submit" className="w-full">
|
||||||
|
Reference in New Issue
Block a user