Skip to content

Commit 52f81b4

Browse files
committed
feat: sigup page
1 parent a99be29 commit 52f81b4

File tree

7 files changed

+273
-25
lines changed

7 files changed

+273
-25
lines changed

apps/web/@/actions/auth/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"use server"
22

3-
import { redirect } from "next/navigation"
4-
3+
import bcrypt from "bcrypt"
54
import { signIn, signOut } from "configs/auth"
65
import { Prisma } from "database"
76
import { createUser } from "database/src/users/queries"
8-
import { z } from "zod"
97

10-
import { SignUpDataOutput } from "./type"
8+
import { redirect } from "@/utils/navigation"
9+
10+
import { SignUpDataOutput, signUpSchema } from "./type"
1111

1212
export const signInWithCredentials = async (email: string, password: string) => {
1313
await signIn("credentials", {
@@ -31,13 +31,20 @@ export const signUp = async (
3131
data: Pick<Prisma.UserCreateInput, "email" | "password">
3232
): Promise<SignUpDataOutput> => {
3333
try {
34-
await createUser({ data })
35-
36-
redirect("/login")
34+
// hash password
35+
const { email, password } = data
36+
const hashedPassword = await bcrypt.hash(password, 10)
37+
38+
await createUser({
39+
data: {
40+
email,
41+
password: hashedPassword,
42+
},
43+
})
3744
} catch (error) {
38-
if (error.code === "P2002") {
45+
if (error?.error?.code === "P2002") {
3946
return {
40-
formErrors: [],
47+
formErrors: null,
4148
fieldErrors: {
4249
email: ["Email already exists"], // TODO: localize error message
4350
},
@@ -49,4 +56,7 @@ export const signUp = async (
4956
fieldErrors: {},
5057
}
5158
}
59+
60+
// TODO: white this redirect not work
61+
redirect("/login")
5262
}

apps/web/@/molecules/auth/sign-in/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type FormData = {
2121
export default function SignIn() {
2222
const t = useTranslations("auth")
2323

24-
const { register, handleSubmit } = useForm<FormData>({
24+
const { register, handleSubmit, formState } = useForm<FormData>({
2525
resolver: zodResolver(
2626
z.object({
2727
email: z.string().email(),
@@ -30,6 +30,8 @@ export default function SignIn() {
3030
),
3131
})
3232

33+
console.log("formState", formState)
34+
3335
const onSignIn = async (data: FormData) => {
3436
await signInWithCredentials(data.email, data.password)
3537
}

apps/web/@/molecules/auth/sign-up/index.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { z } from "zod"
2828

2929
import { signUp } from "@/actions/auth"
3030
import { SignUpDataInput, signUpSchema } from "@/actions/auth/type"
31+
import { redirect } from "@/utils/navigation"
3132

3233
import AuthForm from "../auth-form"
3334

@@ -52,25 +53,25 @@ export default function SignUp() {
5253
// null
5354
// )
5455

55-
console.log(">>>", errors)
56-
5756
const formAction = async ({ confirmPassword, ...data }: SignUpDataInput) => {
5857
const error = await signUp(data)
5958

60-
console.log("formAction>>error", error)
61-
6259
if (error.formErrors) {
6360
toast.error(error.formErrors?.at(0))
64-
}
6561

62+
return
63+
}
6664
if (error.fieldErrors) {
6765
Object?.entries(error.fieldErrors)?.forEach(([field, message]) => {
6866
setError(field, {
6967
type: "manual",
7068
message,
7169
})
7270
})
71+
return
7372
}
73+
74+
redirect("/login")
7475
}
7576

7677
return (
@@ -106,7 +107,7 @@ export default function SignUp() {
106107
name="password"
107108
render={({ field }) => (
108109
<FormItem>
109-
<FormLabel htmlFor="email">{t("password_label")}</FormLabel>
110+
<FormLabel htmlFor="password">{t("password_label")}</FormLabel>
110111
<FormControl>
111112
<Input
112113
id="password"
@@ -125,7 +126,7 @@ export default function SignUp() {
125126
name="confirmPassword"
126127
render={({ field }) => (
127128
<FormItem>
128-
<FormLabel htmlFor="email">{t("confirm_password_label")}</FormLabel>
129+
<FormLabel htmlFor="confirmPassword">{t("confirm_password_label")}</FormLabel>
129130
<FormControl>
130131
<Input
131132
id="confirmPassword"

apps/web/configs/auth.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PrismaAdapter } from "@auth/prisma-adapter"
2+
import bcryptjs from "bcryptjs"
23
import prisma, { getUser } from "database"
34
import NextAuth from "next-auth"
45
import Credentials from "next-auth/providers/credentials"
@@ -24,23 +25,29 @@ export const {
2425
authorize: async (credentials: Record<string, string>) => {
2526
try {
2627
// IMPROVE:
27-
// const { data: user } = await getUser({
28+
const { data: user } = await getUser({
29+
where: {
30+
email: credentials.email,
31+
},
32+
})
33+
34+
// const user = await prisma.user.findUnique({
2835
// where: {
2936
// email: credentials.email,
3037
// password: credentials.password,
3138
// },
3239
// })
33-
const user = await prisma.user.findUnique({
34-
where: {
35-
email: credentials.email,
36-
password: credentials.password,
37-
},
38-
})
3940

4041
if (!user) {
4142
return null
4243
}
4344

45+
const isPasswordValid = await bcryptjs.compare(credentials.password, user.password)
46+
47+
if (!isPasswordValid) {
48+
return null // Invalid password
49+
}
50+
4451
return user
4552
} catch (e) {
4653
return null

apps/web/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
"@tiptap/react": "^2.1.7",
3232
"@tiptap/starter-kit": "^2.1.7",
3333
"autoprefixer": "^10.4.15",
34+
"bcrypt": "^5.1.1",
35+
"bcryptjs": "^2.4.3",
3436
"class-variance-authority": "^0.7.0",
3537
"clsx": "^2.0.0",
3638
"cmdk": "^0.2.1",

packages/database/src/users/selects.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const userDetailSelect = {
1212
totalFollowing: true,
1313
totalPost: true,
1414
totalView: true,
15+
password: true,
1516
} satisfies Prisma.UserSelect
1617

1718
export type TUserDetail = Prisma.UserGetPayload<{

0 commit comments

Comments
 (0)