From 0215644e6c460200fae40d587921b8f40d939b24 Mon Sep 17 00:00:00 2001 From: cfngc4594 Date: Fri, 14 Mar 2025 15:54:32 +0800 Subject: [PATCH] refactor(auth): extract credentials sign-in and sign-up forms into separate components --- src/components/credentials-sign-in.tsx | 28 ++++++++++++++++ src/components/credentials-sign-up.tsx | 45 ++++++++++++++++++++++++++ src/components/sign-in-form.tsx | 25 ++------------ src/components/sign-up-form.tsx | 39 ++-------------------- 4 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 src/components/credentials-sign-in.tsx create mode 100644 src/components/credentials-sign-up.tsx diff --git a/src/components/credentials-sign-in.tsx b/src/components/credentials-sign-in.tsx new file mode 100644 index 0000000..12ed60e --- /dev/null +++ b/src/components/credentials-sign-in.tsx @@ -0,0 +1,28 @@ +import { signIn } from "@/lib/auth"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Button } from "@/components/ui/button"; + +export function CredentialsSignIn() { + return ( +
{ + "use server"; + await signIn("credentials", formData); + }} + > +
+ + +
+
+ + +
+ +
+ ); +} diff --git a/src/components/credentials-sign-up.tsx b/src/components/credentials-sign-up.tsx new file mode 100644 index 0000000..40c02cb --- /dev/null +++ b/src/components/credentials-sign-up.tsx @@ -0,0 +1,45 @@ +import bcrypt from "bcrypt"; +import prisma from "@/lib/prisma"; +import { authSchema } from "@/lib/zod"; +import { redirect } from "next/navigation"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Button } from "@/components/ui/button"; + +export function CredentialsSignUp() { + return ( +
{ + "use server"; + const email = formData.get("email"); + const password = formData.get("password"); + + const validatedData = await authSchema.parseAsync({ email, password }); + const saltRounds = 10; + const pwHash = await bcrypt.hash(validatedData.password, saltRounds); + + await prisma.user.create({ + data: { + email: validatedData.email, + password: pwHash, + }, + }); + + redirect("/sign-in"); + }} + > +
+ + +
+
+ + +
+ +
+ ); +} diff --git a/src/components/sign-in-form.tsx b/src/components/sign-in-form.tsx index 0f14526..8a3e946 100644 --- a/src/components/sign-in-form.tsx +++ b/src/components/sign-in-form.tsx @@ -1,8 +1,5 @@ -import { signIn } from "@/lib/auth"; -import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; -import { Button } from "@/components/ui/button"; import { GithubSignIn } from "@/components/github-sign-in"; +import { CredentialsSignIn } from "@/components/credentials-sign-in"; export function SignInForm() { return ( @@ -13,25 +10,7 @@ export function SignInForm() { Enter your email below to sign in to your account

-
{ - "use server"; - await signIn("credentials", formData); - }} - > -
- - -
-
- - -
- -
+
Or diff --git a/src/components/sign-up-form.tsx b/src/components/sign-up-form.tsx index ef64820..10e18ce 100644 --- a/src/components/sign-up-form.tsx +++ b/src/components/sign-up-form.tsx @@ -1,11 +1,5 @@ -import bcrypt from "bcrypt"; -import prisma from "@/lib/prisma"; -import { authSchema } from "@/lib/zod"; -import { redirect } from "next/navigation"; -import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; -import { Button } from "@/components/ui/button"; import { GithubSignIn } from "@/components/github-sign-in"; +import { CredentialsSignUp } from "@/components/credentials-sign-up"; export function SignUpForm() { return ( @@ -16,36 +10,7 @@ export function SignUpForm() { Enter your email below to sign up to your account

-
{ - "use server"; - const email = formData.get("email"); - const password = formData.get("password"); - const validatedData = await authSchema.parseAsync({ email, password }); - const saltRounds = 10; - const pwHash = await bcrypt.hash(validatedData.password, saltRounds); - await prisma.user.create({ - data: { - email: validatedData.email, - password: pwHash - } - }) - redirect("/sign-in"); - }} - > -
- - -
-
- - -
- -
+
Or