From 468e72e9a86ec17e38347e20fd1e5213e87760d2 Mon Sep 17 00:00:00 2001 From: cfngc4594 Date: Fri, 14 Mar 2025 17:06:19 +0800 Subject: [PATCH] feat(auth): add sign up and sign in functions with credentials --- src/app/actions/auth.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/app/actions/auth.ts diff --git a/src/app/actions/auth.ts b/src/app/actions/auth.ts new file mode 100644 index 0000000..cf58c9a --- /dev/null +++ b/src/app/actions/auth.ts @@ -0,0 +1,26 @@ +"use server"; + +import bcrypt from "bcrypt"; +import prisma from "@/lib/prisma"; +import { signIn } from "@/lib/auth"; +import { authSchema } from "@/lib/zod"; +import { redirect } from "next/navigation"; + +export async function signInWithCredentials(formData: { email: string; password: string }) { + await signIn("credentials", formData); +} + +export async function signUpWithCredentials(formData: { email: string; password: string }) { + const validatedData = await authSchema.parseAsync(formData); + const saltRounds = 10; + const pwHash = await bcrypt.hash(validatedData.password, saltRounds); + + await prisma.user.create({ + data: { + email: validatedData.email, + password: pwHash, + }, + }); + + redirect("/sign-in"); +}