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.

Fix: login error validation

Khuram Niaz7df090a6b899
diff --git a/src/app/(auth)/signin/page.tsx b/src/app/(auth)/signin/page.tsx
index 6f0c934..b873cc1 100644
--- a/src/app/(auth)/signin/page.tsx
+++ b/src/app/(auth)/signin/page.tsx
@@ -1,7 +1,5 @@
-"use client";
import Link from "next/link";
-import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
@@ -9,45 +7,14 @@ import {
CardHeader,
CardTitle,
} from "@/components/ui/card";
-import { Input } from "@/components/ui/input";
-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";
-import { useFormState, useFormStatus } from "react-dom";
-import { ArrowRightIcon } from "lucide-react";
-import { authenticate } from "@/actions/auth.actions";
+import { Metadata } from "next";
+import SigninForm from "@/components/SigninForm";
-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 const metadata: Metadata = {
+ title: "Signin",
+};
export default function Signin() {
- const form = useForm<z.infer<typeof FormSchema>>({
- resolver: zodResolver(FormSchema),
- });
-
- const [errorMessage, dispatch] = useFormState(authenticate, undefined);
-
- const { pending } = useFormStatus();
-
return (
<Card className="mx-auto max-w-sm">
<CardHeader>
@@ -57,66 +24,7 @@ export default function Signin() {
</CardDescription>
</CardHeader>
<CardContent>
- <Form {...form}>
- <form
- action={dispatch}
- // 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 aria-disabled={pending} className="w-full">
- Login
- </Button>
- <div
- className="flex h-8 items-end space-x-1"
- aria-live="polite"
- aria-atomic="true"
- >
- {errorMessage && (
- <>
- <p className="text-sm text-red-500">{errorMessage}</p>
- </>
- )}
- </div>
- </div>
- </form>
- </Form>
+ <SigninForm />
<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 19fbaab..9779f7a 100644
--- a/src/app/(auth)/signup/page.tsx
+++ b/src/app/(auth)/signup/page.tsx
@@ -66,8 +66,7 @@ export default function Signup() {
<CardContent>
<Form {...form}>
<form
- onSubmit={form.handleSubmit(onSubmit)}
- // className="w-2/3 space-y-6"
+ // className="w-2/3 space-y-6"
>
<div className="grid gap-4">
<div className="grid gap-2">
diff --git a/src/app/dashboard/myjobs/page.tsx b/src/app/dashboard/myjobs/page.tsx
index 1ff45d3..e2ee54f 100644
--- a/src/app/dashboard/myjobs/page.tsx
+++ b/src/app/dashboard/myjobs/page.tsx
@@ -28,6 +28,11 @@ import {
TableRow,
} from "@/components/ui/table";
import { MY_JOBS_DATA } from "@/lib/myJobsData";
+import { Metadata } from "next";
+
+export const metadata: Metadata = {
+ title: "My Jobs | JobSync",
+};
function MyJobs() {
return (
diff --git a/src/app/dashboard/page.tsx b/src/app/dashboard/page.tsx
index 435a618..8f12db6 100644
--- a/src/app/dashboard/page.tsx
+++ b/src/app/dashboard/page.tsx
@@ -5,6 +5,12 @@ import RecentJobsCard from "@/components/RecentJobsCard";
import WeeklyBarChart from "@/components/WeeklyBarChart";
import { calendarData } from "@/lib/calendarData";
+import { Metadata } from "next";
+
+export const metadata: Metadata = {
+ title: "Dashboard",
+};
+
export default function Dashboard() {
return (
<>
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index 29247cd..7331bd9 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -10,7 +10,10 @@ const fontSans = FontSans({
});
export const metadata: Metadata = {
- title: "JobSync App",
+ title: {
+ template: "%s | JobSync",
+ default: "JobSync",
+ },
description: "Job Application Tracking System",
};
diff --git a/src/auth.ts b/src/auth.ts
index 08a0812..b9ea7ad 100644
--- a/src/auth.ts
+++ b/src/auth.ts
@@ -2,11 +2,9 @@ import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { z } from "zod";
var bcrypt = require("bcryptjs");
-import { PrismaClient } from "@prisma/client";
import { authConfig } from "./auth.config";
import { User } from "./models/user";
-
-const prisma = new PrismaClient();
+import prisma from "./lib/db";
async function getUser(email: string): Promise<User | undefined> {
try {
diff --git a/src/components/Header.tsx b/src/components/Header.tsx
index 89409bb..78103ab 100644
--- a/src/components/Header.tsx
+++ b/src/components/Header.tsx
@@ -1,6 +1,6 @@
import Image from "next/image";
import Link from "next/link";
-import { PanelLeft, Briefcase, Search } from "lucide-react";
+import { PanelLeft, Briefcase, Search, PowerIcon } from "lucide-react";
import { Input } from "./ui/input";
import { Button } from "./ui/button";
@@ -20,6 +20,7 @@ import {
} from "@/components/ui/sheet";
import { ModeToggle } from "./ModeToggle";
import { SIDEBAR_LINKS } from "@/lib/constants";
+import { signOut } from "@/auth";
function Header() {
return (
@@ -90,7 +91,19 @@ function Header() {
<DropdownMenuItem>Settings</DropdownMenuItem>
<DropdownMenuItem>Support</DropdownMenuItem>
<DropdownMenuSeparator />
- <DropdownMenuItem>Logout</DropdownMenuItem>
+ <form
+ action={async () => {
+ "use server";
+ await signOut();
+ }}
+ >
+ <DropdownMenuItem>
+ <Button variant="ghost" className="w-full">
+ <PowerIcon className="w-5" />
+ <div className="hidden md:block mx-2">Logout</div>
+ </Button>
+ </DropdownMenuItem>
+ </form>
</DropdownMenuContent>
</DropdownMenu>
<ModeToggle />
diff --git a/src/components/SigninForm.tsx b/src/components/SigninForm.tsx
new file mode 100644
index 0000000..3815a58
--- /dev/null
+++ b/src/components/SigninForm.tsx
@@ -0,0 +1,129 @@
+"use client";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { useForm } from "react-hook-form";
+import { z } from "zod";
+import { useFormStatus } from "react-dom";
+import { authenticate } from "@/actions/auth.actions";
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form";
+import { Button } from "./ui/button";
+import { Input } from "./ui/input";
+import { useState } from "react";
+import { useRouter } from "next/navigation";
+
+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.",
+ })
+ .min(1),
+});
+
+function SigninForm() {
+ const form = useForm<z.infer<typeof FormSchema>>({
+ resolver: zodResolver(FormSchema),
+ mode: "onChange",
+ });
+
+ const [errorMessage, setError] = useState("");
+ const router = useRouter();
+
+ const { pending } = useFormStatus();
+
+ const getFormData = (object: Object) =>
+ Object.keys(object).reduce((formData, key) => {
+ formData.append(key, object[key as keyof object]);
+ return formData;
+ }, new FormData());
+
+ const onSubmit = async (data: z.infer<typeof FormSchema>) => {
+ const formData = getFormData(data);
+ const errorResponse = await authenticate("", formData);
+ if (!!errorResponse) {
+ setError(errorResponse);
+ } else {
+ router.push("/dashboard");
+ }
+ };
+
+ return (
+ <>
+ <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" aria-disabled={pending} className="w-full">
+ Login
+ </Button>
+ <div
+ className="flex h-8 items-end space-x-1"
+ aria-live="polite"
+ aria-atomic="true"
+ >
+ {errorMessage && (
+ <>
+ <p className="text-sm text-red-500">{errorMessage}</p>
+ </>
+ )}
+ </div>
+ </div>
+ </form>
+ </Form>
+ </>
+ );
+}
+
+export default SigninForm;
diff --git a/src/lib/db.ts b/src/lib/db.ts
new file mode 100644
index 0000000..e5d7482
--- /dev/null
+++ b/src/lib/db.ts
@@ -0,0 +1,15 @@
+import { PrismaClient } from "@prisma/client";
+
+const prismaClientSingleton = () => {
+ return new PrismaClient();
+};
+
+declare const globalThis: {
+ prismaGlobal: ReturnType<typeof prismaClientSingleton>;
+} & typeof global;
+
+const prisma = globalThis.prismaGlobal ?? prismaClientSingleton();
+
+export default prisma;
+
+if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma;