From 8ede08bb99f7029f553350c55bec8d527cd9cf79 Mon Sep 17 00:00:00 2001 From: Don Strawsburg Date: Wed, 15 Jan 2025 17:17:20 -0500 Subject: [PATCH] users email duplicate check --- src/actions/userActions.ts | 17 +++++++++++------ src/components/RegistrationForm/index.tsx | 14 +++++++++----- src/drizzle/schema/schema.ts | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/actions/userActions.ts b/src/actions/userActions.ts index 90a10a2..b8eb7a9 100644 --- a/src/actions/userActions.ts +++ b/src/actions/userActions.ts @@ -2,27 +2,32 @@ import { eq, not , asc} from "drizzle-orm"; import { revalidatePath } from "next/cache"; import { db } from "@db/index"; -import { usersMerged } from "@schemas/schema"; +import { users } from "@schemas/schema"; export const getData = async () => { - const data = await db.select().from(usersMerged).orderBy(asc(usersMerged.email)); + const data = await db.select().from(users).orderBy(asc(users.email)); + return data; +}; + +export const getUserByEmail = async (email:string) => { + const data = await db.select().from(users).where(eq(users.email, email)); return data; }; export const addUser = async ( first_name: string, last_name: string, username: string, email: string, password_hash : string) => { - await db.insert(usersMerged).values({ + await db.insert(users).values({ first_name : first_name, last_name: last_name, username: username, email: email, password_hash : password_hash }); }; export const deleteUser = async (id: string) => { - await db.delete(usersMerged).where(eq(usersMerged.id, id)); + await db.delete(users).where(eq(users.id, id)); revalidatePath("/"); }; export const editUser = async (id: string, first_name: string, last_name: string, username: string, email : string, password_hash: string) => { await db - .update(usersMerged) + .update(users) .set({ first_name : first_name, last_name: last_name, @@ -30,6 +35,6 @@ export const editUser = async (id: string, first_name: string, last_name: string email: email, password_hash: password_hash }) - .where(eq(usersMerged.id, id)); + .where(eq(users.id, id)); revalidatePath("/"); }; \ No newline at end of file diff --git a/src/components/RegistrationForm/index.tsx b/src/components/RegistrationForm/index.tsx index 729b8e9..5b59f86 100644 --- a/src/components/RegistrationForm/index.tsx +++ b/src/components/RegistrationForm/index.tsx @@ -2,7 +2,8 @@ import { useState } from "react"; import { useRouter } from "next/navigation"; -import { addUser } from "@actions/userActions"; +import { addUser, getUserByEmail } from "@actions/userActions"; +import { users } from '@schemas/schema'; export default function RegistrationForm() { const router = useRouter(); const [error, setError] = useState(""); @@ -18,15 +19,19 @@ export default function RegistrationForm() { const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); - alert("in the submit"); + alert(formData.password + ":" + formData.confirmPassword); if (formData.password !== formData.confirmPassword) { setError("Passwords do not match"); - console.log("password no match"); - alert("password no match"); return; } + const existingUser : any = null; + if((await getUserByEmail(formData.email)).length > 0){ + setError("Duplicate E-Mail Address"); + return + } + try { addUser( formData.first_name, @@ -35,7 +40,6 @@ export default function RegistrationForm() { formData.email, formData.password ); - console.log("Form submitted:", formData); router.push("/signin"); // Redirect to login after successful registration } catch (err) { setError("Failed to register user"); diff --git a/src/drizzle/schema/schema.ts b/src/drizzle/schema/schema.ts index b5bc825..ffd5902 100644 --- a/src/drizzle/schema/schema.ts +++ b/src/drizzle/schema/schema.ts @@ -17,7 +17,7 @@ export const products = pgTable("products", { }); -export const usersMerged = pgTable("users", { +export const users = pgTable("users", { id: text("id") .primaryKey() .$defaultFn(() => crypto.randomUUID()),