@@ -13,27 +13,48 @@ import {
13
13
FormMessage ,
14
14
FormField ,
15
15
} from "@/components/ui/form" ;
16
-
17
- interface FormData {
18
- username : string ;
19
- email : string ;
20
- password : string ;
21
- confirmPassword : string ;
22
- }
16
+ import { z } from "zod" ;
17
+ import { signup } from "@/services/authService" ;
18
+ import { useRouter } from "next/navigation" ;
19
+ import { zodResolver } from "@hookform/resolvers/zod" ;
20
+
21
+ const SignupFormSchema = z
22
+ . object ( {
23
+ username : z
24
+ . string ( )
25
+ . min ( 2 , { message : "Name must be at least 2 characters long." } )
26
+ . trim ( ) ,
27
+ email : z . string ( ) . email ( { message : "Please enter a valid email." } ) . trim ( ) ,
28
+ password : z
29
+ . string ( )
30
+ . min ( 8 , { message : "Be at least 8 characters long" } )
31
+ . regex ( / [ a - z A - Z ] / , { message : "Contain at least one letter." } )
32
+ . regex ( / [ 0 - 9 ] / , { message : "Contain at least one number." } )
33
+ . trim ( ) ,
34
+ confirmPassword : z . string ( ) . trim ( ) ,
35
+ } )
36
+ . refine ( ( data ) => data . password === data . confirmPassword , {
37
+ message : "Passwords must match" ,
38
+ path : [ "confirmPassword" ] ,
39
+ } ) ;
23
40
24
41
export default function SignUpPage ( ) {
25
- const methods = useForm < FormData > ( {
26
- defaultValues : {
27
- username : '' ,
28
- email : '' ,
29
- password : '' ,
30
- confirmPassword : '' ,
31
- } ,
42
+ const router = useRouter ( ) ;
43
+
44
+ const methods = useForm < z . infer < typeof SignupFormSchema > > ( {
45
+ resolver : zodResolver ( SignupFormSchema ) ,
46
+ defaultValues : { } ,
32
47
} ) ;
33
48
34
- const { handleSubmit, control, formState : { errors } } = methods ;
49
+ const {
50
+ handleSubmit,
51
+ control,
52
+ formState : { errors } ,
53
+ } = methods ;
35
54
36
- const onSubmit : SubmitHandler < FormData > = async ( data ) => {
55
+ const onSubmit : SubmitHandler < z . infer < typeof SignupFormSchema > > = async (
56
+ data
57
+ ) => {
37
58
if ( data . password !== data . confirmPassword ) {
38
59
methods . setError ( "confirmPassword" , {
39
60
type : "manual" ,
@@ -42,20 +63,15 @@ export default function SignUpPage() {
42
63
return ;
43
64
}
44
65
45
- try {
46
- const response = await fetch ( '/api/signup' , {
47
- method : 'POST' ,
48
- headers : { 'Content-Type' : 'application/json' } ,
49
- body : JSON . stringify ( data ) ,
50
- } ) ;
51
-
52
- if ( response . ok ) {
53
- alert ( 'Sign up successful!' ) ;
54
- } else {
55
- alert ( 'Sign up failed.' ) ;
56
- }
57
- } catch ( error ) {
58
- console . error ( 'Error:' , error ) ;
66
+ const accessTokenResponse = await signup ( data ) ;
67
+ if ( accessTokenResponse . statusCode === 200 && accessTokenResponse . data ) {
68
+ localStorage . setItem (
69
+ "access_token" ,
70
+ accessTokenResponse . data . access_token
71
+ ) ;
72
+ router . push ( "/dashboard" ) ;
73
+ } else {
74
+ // TODO: Display error message
59
75
}
60
76
} ;
61
77
@@ -73,7 +89,6 @@ export default function SignUpPage() {
73
89
< FormProvider { ...methods } >
74
90
< form onSubmit = { handleSubmit ( onSubmit ) } >
75
91
< div className = "flex flex-col gap-y-2" >
76
-
77
92
{ /* Username Field */ }
78
93
< FormField
79
94
control = { control }
@@ -91,7 +106,9 @@ export default function SignUpPage() {
91
106
/>
92
107
</ div >
93
108
</ FormControl >
94
- { errors . username && < FormMessage > { errors . username . message } </ FormMessage > }
109
+ { errors . username && (
110
+ < FormMessage > { errors . username . message } </ FormMessage >
111
+ ) }
95
112
</ FormItem >
96
113
) }
97
114
/>
@@ -114,7 +131,9 @@ export default function SignUpPage() {
114
131
/>
115
132
</ div >
116
133
</ FormControl >
117
- { errors . email && < FormMessage > { errors . email . message } </ FormMessage > }
134
+ { errors . email && (
135
+ < FormMessage > { errors . email . message } </ FormMessage >
136
+ ) }
118
137
</ FormItem >
119
138
) }
120
139
/>
@@ -137,7 +156,9 @@ export default function SignUpPage() {
137
156
/>
138
157
</ div >
139
158
</ FormControl >
140
- { errors . password && < FormMessage > { errors . password . message } </ FormMessage > }
159
+ { errors . password && (
160
+ < FormMessage > { errors . password . message } </ FormMessage >
161
+ ) }
141
162
</ FormItem >
142
163
) }
143
164
/>
@@ -160,15 +181,22 @@ export default function SignUpPage() {
160
181
/>
161
182
</ div >
162
183
</ FormControl >
163
- { errors . confirmPassword && < FormMessage > { errors . confirmPassword . message } </ FormMessage > }
184
+ { errors . confirmPassword && (
185
+ < FormMessage >
186
+ { errors . confirmPassword . message }
187
+ </ FormMessage >
188
+ ) }
164
189
</ FormItem >
165
190
) }
166
191
/>
167
192
</ div >
168
193
169
194
{ /* Forgot password link */ }
170
195
< div className = "text-right pt-2 pb-2" >
171
- < Link href = "/forgotpassword" className = "hover:underline text-sm" >
196
+ < Link
197
+ href = "/forgotpassword"
198
+ className = "hover:underline text-sm"
199
+ >
172
200
Forgot password?
173
201
</ Link >
174
202
</ div >
0 commit comments