users email duplicate check

This commit is contained in:
2025-01-15 17:17:20 -05:00
parent aabaa586bf
commit 8ede08bb99
3 changed files with 21 additions and 12 deletions

View File

@@ -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("/");
};

View File

@@ -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");

View File

@@ -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()),