Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

add signin,signup form validation

Khuram Niazd8a4c4a1b674
diff --git a/package-lock.json b/package-lock.json
index 85600a9..8cd980d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8,6 +8,7 @@
"name": "jobsync",
"version": "0.1.0",
"dependencies": {
+ "@hookform/resolvers": "^3.4.2",
"@nivo/bar": "^0.87.0",
"@nivo/calendar": "^0.87.0",
"@nivo/core": "^0.87.0",
@@ -26,8 +27,10 @@
"next-themes": "^0.3.0",
"react": "^18",
"react-dom": "^18",
+ "react-hook-form": "^7.51.5",
"tailwind-merge": "^2.3.0",
- "tailwindcss-animate": "^1.0.7"
+ "tailwindcss-animate": "^1.0.7",
+ "zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20",
@@ -153,6 +156,14 @@
"resolved": "https://registry.npmjs.org/@floating-ui/utils/-/utils-0.2.2.tgz",
"integrity": "sha512-J4yDIIthosAsRZ5CPYP/jQvUAQtlZTTD/4suA08/FEnlxqW3sKS9iAhgsa9VYLZ6vDHn/ixJgIqRQPotoBjxIw=="
},
+ "node_modules/@hookform/resolvers": {
+ "version": "3.4.2",
+ "resolved": "https://registry.npmjs.org/@hookform/resolvers/-/resolvers-3.4.2.tgz",
+ "integrity": "sha512-1m9uAVIO8wVf7VCDAGsuGA0t6Z3m6jVGAN50HkV9vYLl0yixKK/Z1lr01vaRvYCkIKGoy1noVRxMzQYb4y/j1Q==",
+ "peerDependencies": {
+ "react-hook-form": "^7.0.0"
+ }
+ },
"node_modules/@humanwhocodes/config-array": {
"version": "0.11.14",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.14.tgz",
@@ -4942,6 +4953,21 @@
"react": "^18.3.1"
}
},
+ "node_modules/react-hook-form": {
+ "version": "7.51.5",
+ "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.51.5.tgz",
+ "integrity": "sha512-J2ILT5gWx1XUIJRETiA7M19iXHlG74+6O3KApzvqB/w8S5NQR7AbU8HVZrMALdmDgWpRPYiZJl0zx8Z4L2mP6Q==",
+ "engines": {
+ "node": ">=12.22.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/react-hook-form"
+ },
+ "peerDependencies": {
+ "react": "^16.8.0 || ^17 || ^18"
+ }
+ },
"node_modules/react-is": {
"version": "16.13.1",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
@@ -6131,6 +6157,14 @@
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
+ },
+ "node_modules/zod": {
+ "version": "3.23.8",
+ "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz",
+ "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==",
+ "funding": {
+ "url": "https://github.com/sponsors/colinhacks"
+ }
}
}
}
diff --git a/package.json b/package.json
index cc421f9..feab765 100644
--- a/package.json
+++ b/package.json
@@ -9,6 +9,7 @@
"lint": "next lint"
},
"dependencies": {
+ "@hookform/resolvers": "^3.4.2",
"@nivo/bar": "^0.87.0",
"@nivo/calendar": "^0.87.0",
"@nivo/core": "^0.87.0",
@@ -27,8 +28,10 @@
"next-themes": "^0.3.0",
"react": "^18",
"react-dom": "^18",
+ "react-hook-form": "^7.51.5",
"tailwind-merge": "^2.3.0",
- "tailwindcss-animate": "^1.0.7"
+ "tailwindcss-animate": "^1.0.7",
+ "zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20",
diff --git a/src/app/(auth)/signin/page.tsx b/src/app/(auth)/signin/page.tsx
index 5c424a3..179393a 100644
--- a/src/app/(auth)/signin/page.tsx
+++ b/src/app/(auth)/signin/page.tsx
@@ -1,3 +1,4 @@
+"use client";
import Link from "next/link";
import { Button } from "@/components/ui/button";
@@ -9,9 +10,41 @@ import {
CardTitle,
} from "@/components/ui/card";
import { Input } from "@/components/ui/input";
-import { Label } from "@/components/ui/label";
+import { z } from "zod";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { useForm } from "react-hook-form";
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form";
+
+const FormSchema = z.object({
+ email: z
+ .string({
+ required_error: "Email is required.",
+ })
+ .min(3, {
+ message: "Email must be at least 3 characters.",
+ })
+ .email("Please enter a valid email."),
+ password: z.string({
+ required_error: "Please enter your password.",
+ }),
+});
export default function Signin() {
+ const form = useForm<z.infer<typeof FormSchema>>({
+ resolver: zodResolver(FormSchema),
+ });
+
+ function onSubmit(data: z.infer<typeof FormSchema>) {
+ console.log("onsubmit data: ", data);
+ }
+
return (
<Card className="mx-auto max-w-sm">
<CardHeader>
@@ -21,29 +54,55 @@ export default function Signin() {
</CardDescription>
</CardHeader>
<CardContent>
- <div className="grid gap-4">
- <div className="grid gap-2">
- <Label htmlFor="email">Email</Label>
- <Input
- id="email"
- type="email"
- placeholder="[email protected]"
- required
- />
- </div>
- <div className="grid gap-2">
- <div className="flex items-center">
- <Label htmlFor="password">Password</Label>
- {/* <Link href="#" className="ml-auto inline-block text-sm underline">
+ <Form {...form}>
+ <form
+ onSubmit={form.handleSubmit(onSubmit)}
+ // className="w-2/3 space-y-6"
+ >
+ <div className="grid gap-4">
+ <div className="grid gap-2">
+ <FormField
+ control={form.control}
+ name="email"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Email</FormLabel>
+ <FormControl>
+ <Input placeholder="[email protected]" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </div>
+ <div className="grid gap-2">
+ <FormField
+ control={form.control}
+ name="password"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Password</FormLabel>
+ <FormControl>
+ <Input type="password" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ {/* <div className="flex items-center"> */}
+ {/* <Label htmlFor="password">Password</Label> */}
+ {/* <Link href="#" className="ml-auto inline-block text-sm underline">
Forgot your password?
</Link> */}
+ {/* </div> */}
+ {/* <Input id="password" type="password" required /> */}
+ </div>
+ <Button type="submit" className="w-full">
+ Login
+ </Button>
</div>
- <Input id="password" type="password" required />
- </div>
- <Button type="submit" className="w-full">
- Login
- </Button>
- </div>
+ </form>
+ </Form>
<div className="mt-4 text-center text-sm">
Don&apos;t have an account?{" "}
<Link href="/signup" className="underline">
diff --git a/src/app/(auth)/signup/page.tsx b/src/app/(auth)/signup/page.tsx
index bca017e..19fbaab 100644
--- a/src/app/(auth)/signup/page.tsx
+++ b/src/app/(auth)/signup/page.tsx
@@ -1,3 +1,4 @@
+"use client";
import Link from "next/link";
import { Button } from "@/components/ui/button";
@@ -9,9 +10,51 @@ import {
CardTitle,
} from "@/components/ui/card";
import { Input } from "@/components/ui/input";
-import { Label } from "@/components/ui/label";
+import { z } from "zod";
+import { useForm } from "react-hook-form";
+import { zodResolver } from "@hookform/resolvers/zod";
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form";
+
+const FormSchema = z.object({
+ name: z.string().min(2, {
+ message: "Name must be at least 2 characters.",
+ }),
+ email: z
+ .string({
+ required_error: "Email is required.",
+ })
+ .min(3, {
+ message: "Email must be at least 3 characters.",
+ })
+ .email("Please enter a valid email."),
+ password: z
+ .string({
+ required_error: "Please enter your password.",
+ })
+ .min(6, {
+ message: "Name must be at least 6 characters.",
+ }),
+});
export default function Signup() {
+ const form = useForm<z.infer<typeof FormSchema>>({
+ resolver: zodResolver(FormSchema),
+ // defaultValues: {
+ // username: "",
+ // },
+ });
+
+ function onSubmit(data: z.infer<typeof FormSchema>) {
+ console.log("onsubmit data: ", data);
+ }
+
return (
<Card className="mx-auto max-w-sm">
<CardHeader>
@@ -21,28 +64,64 @@ export default function Signup() {
</CardDescription>
</CardHeader>
<CardContent>
- <div className="grid gap-4">
- <div className="grid gap-2">
- <Label htmlFor="last-name">Full Name</Label>
- <Input id="last-name" placeholder="Your Name" required />
- </div>
- <div className="grid gap-2">
- <Label htmlFor="email">Email</Label>
- <Input
- id="email"
- type="email"
- placeholder="[email protected]"
- required
- />
- </div>
- <div className="grid gap-2">
- <Label htmlFor="password">Password</Label>
- <Input id="password" type="password" />
- </div>
- <Button type="submit" className="w-full">
- Create an account
- </Button>
- </div>
+ <Form {...form}>
+ <form
+ onSubmit={form.handleSubmit(onSubmit)}
+ // className="w-2/3 space-y-6"
+ >
+ <div className="grid gap-4">
+ <div className="grid gap-2">
+ <FormField
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Full Name</FormLabel>
+ <FormControl>
+ <Input placeholder="Your Name" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </div>
+ <div className="grid gap-2">
+ <FormField
+ control={form.control}
+ name="email"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Email</FormLabel>
+ <FormControl>
+ <Input placeholder="[email protected]" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </div>
+ <div className="grid gap-2">
+ <FormField
+ control={form.control}
+ name="password"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Password</FormLabel>
+ <FormControl>
+ <Input type="password" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </div>
+ <Button type="submit" className="w-full">
+ Create an account
+ </Button>
+ </div>
+ </form>
+ </Form>
+
<div className="mt-4 text-center text-sm">
Already have an account?{" "}
<Link href="/signin" className="underline">
diff --git a/src/app/globals.css b/src/app/globals.css
index 3619048..4449064 100644
--- a/src/app/globals.css
+++ b/src/app/globals.css
@@ -41,10 +41,10 @@
--muted-foreground: 215 20.2% 65.1%;
--accent: 217.2 32.6% 17.5%;
--accent-foreground: 210 40% 98%;
- --destructive: 0 62.8% 30.6%;
+ --destructive: 0 62.8% 40.6%;
--destructive-foreground: 210 40% 98%;
- --border: 217.2 32.6% 17.5%;
- --input: 217.2 32.6% 17.5%;
+ --border: 217.2 32.6% 21.5%;
+ --input: 217.2 32.6% 21.5%;
--ring: 224.3 76.3% 48%;
}
}
diff --git a/src/components/ui/form.tsx b/src/components/ui/form.tsx
new file mode 100644
index 0000000..4603f8b
--- /dev/null
+++ b/src/components/ui/form.tsx
@@ -0,0 +1,176 @@
+import * as React from "react"
+import * as LabelPrimitive from "@radix-ui/react-label"
+import { Slot } from "@radix-ui/react-slot"
+import {
+ Controller,
+ ControllerProps,
+ FieldPath,
+ FieldValues,
+ FormProvider,
+ useFormContext,
+} from "react-hook-form"
+
+import { cn } from "@/lib/utils"
+import { Label } from "@/components/ui/label"
+
+const Form = FormProvider
+
+type FormFieldContextValue<
+ TFieldValues extends FieldValues = FieldValues,
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
+> = {
+ name: TName
+}
+
+const FormFieldContext = React.createContext<FormFieldContextValue>(
+ {} as FormFieldContextValue
+)
+
+const FormField = <
+ TFieldValues extends FieldValues = FieldValues,
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
+>({
+ ...props
+}: ControllerProps<TFieldValues, TName>) => {
+ return (
+ <FormFieldContext.Provider value={{ name: props.name }}>
+ <Controller {...props} />
+ </FormFieldContext.Provider>
+ )
+}
+
+const useFormField = () => {
+ const fieldContext = React.useContext(FormFieldContext)
+ const itemContext = React.useContext(FormItemContext)
+ const { getFieldState, formState } = useFormContext()
+
+ const fieldState = getFieldState(fieldContext.name, formState)
+
+ if (!fieldContext) {
+ throw new Error("useFormField should be used within <FormField>")
+ }
+
+ const { id } = itemContext
+
+ return {
+ id,
+ name: fieldContext.name,
+ formItemId: `${id}-form-item`,
+ formDescriptionId: `${id}-form-item-description`,
+ formMessageId: `${id}-form-item-message`,
+ ...fieldState,
+ }
+}
+
+type FormItemContextValue = {
+ id: string
+}
+
+const FormItemContext = React.createContext<FormItemContextValue>(
+ {} as FormItemContextValue
+)
+
+const FormItem = React.forwardRef<
+ HTMLDivElement,
+ React.HTMLAttributes<HTMLDivElement>
+>(({ className, ...props }, ref) => {
+ const id = React.useId()
+
+ return (
+ <FormItemContext.Provider value={{ id }}>
+ <div ref={ref} className={cn("space-y-2", className)} {...props} />
+ </FormItemContext.Provider>
+ )
+})
+FormItem.displayName = "FormItem"
+
+const FormLabel = React.forwardRef<
+ React.ElementRef<typeof LabelPrimitive.Root>,
+ React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
+>(({ className, ...props }, ref) => {
+ const { error, formItemId } = useFormField()
+
+ return (
+ <Label
+ ref={ref}
+ className={cn(error && "text-destructive", className)}
+ htmlFor={formItemId}
+ {...props}
+ />
+ )
+})
+FormLabel.displayName = "FormLabel"
+
+const FormControl = React.forwardRef<
+ React.ElementRef<typeof Slot>,
+ React.ComponentPropsWithoutRef<typeof Slot>
+>(({ ...props }, ref) => {
+ const { error, formItemId, formDescriptionId, formMessageId } = useFormField()
+
+ return (
+ <Slot
+ ref={ref}
+ id={formItemId}
+ aria-describedby={
+ !error
+ ? `${formDescriptionId}`
+ : `${formDescriptionId} ${formMessageId}`
+ }
+ aria-invalid={!!error}
+ {...props}
+ />
+ )
+})
+FormControl.displayName = "FormControl"
+
+const FormDescription = React.forwardRef<
+ HTMLParagraphElement,
+ React.HTMLAttributes<HTMLParagraphElement>
+>(({ className, ...props }, ref) => {
+ const { formDescriptionId } = useFormField()
+
+ return (
+ <p
+ ref={ref}
+ id={formDescriptionId}
+ className={cn("text-sm text-muted-foreground", className)}
+ {...props}
+ />
+ )
+})
+FormDescription.displayName = "FormDescription"
+
+const FormMessage = React.forwardRef<
+ HTMLParagraphElement,
+ React.HTMLAttributes<HTMLParagraphElement>
+>(({ className, children, ...props }, ref) => {
+ const { error, formMessageId } = useFormField()
+ const body = error ? String(error?.message) : children
+
+ if (!body) {
+ return null
+ }
+
+ return (
+ <p
+ ref={ref}
+ id={formMessageId}
+ className={cn("text-sm font-medium text-destructive", className)}
+ {...props}
+ >
+ {body}
+ </p>
+ )
+})
+FormMessage.displayName = "FormMessage"
+
+export {
+ useFormField,
+ Form,
+ FormItem,
+ FormLabel,
+ FormControl,
+ FormDescription,
+ FormMessage,
+ FormField,
+}