2
2
3
3
import { register } from "@/api/user" ;
4
4
import { Button } from "@/components/ui/button" ;
5
- import { Form , FormControl , FormField , FormItem , FormLabel , FormMessage } from "@/components/ui/form" ;
5
+ import {
6
+ Form ,
7
+ FormControl ,
8
+ FormField ,
9
+ FormItem ,
10
+ FormLabel ,
11
+ FormMessage ,
12
+ } from "@/components/ui/form" ;
6
13
import { Input } from "@/components/ui/input" ;
7
14
import { zodResolver } from "@hookform/resolvers/zod" ;
8
15
import { useForm } from "react-hook-form" ;
@@ -13,14 +20,22 @@ import { useRouter } from "next/navigation";
13
20
import { useEffect , useState } from "react" ;
14
21
15
22
const formSchema = z . object ( {
16
- username : z . string ( )
23
+ username : z
24
+ . string ( )
17
25
. min ( 5 , "Username must be at least 5 characters" )
18
26
. max ( 20 , "Username must be at most 20 characters" ) ,
19
- email : z . string ( )
20
- . email ( "Invalid email" ) ,
21
- password : z . string ( )
27
+ email : z . string ( ) . email ( "Invalid email" ) ,
28
+ password : z
29
+ . string ( )
22
30
. min ( 8 , "Password must be at least 8 characters" )
23
- . max ( 100 , "Password must be at most 100 characters" ) ,
31
+ . max ( 100 , "Password must be at most 100 characters" )
32
+ . regex ( / [ a - z ] / , "Password must contain at least one lowercase letter" )
33
+ . regex ( / [ A - Z ] / , "Password must contain at least one uppercase letter" )
34
+ . regex ( / \d / , "Password must contain at least one number" )
35
+ . regex (
36
+ / [ @ $ ! % * ? & ] / ,
37
+ "Password must contain at least one special character (@, $, !, %, *, ?, &)"
38
+ ) ,
24
39
} ) ;
25
40
26
41
const Register = ( ) => {
@@ -41,15 +56,19 @@ const Register = () => {
41
56
} ) ;
42
57
43
58
const onSubmit = async ( data : z . infer < typeof formSchema > ) => {
44
- const registerIsSuccessful = await register ( data . email , data . password , data . username ) ;
59
+ const registerIsSuccessful = await register (
60
+ data . email ,
61
+ data . password ,
62
+ data . username
63
+ ) ;
45
64
if ( ! registerIsSuccessful ) return ;
46
65
47
66
// redirect to /login
48
67
Swal . fire ( {
49
68
title : "Account created!" ,
50
69
text : "You can now login to your account." ,
51
70
icon : "success" ,
52
- confirmButtonText : "Login"
71
+ confirmButtonText : "Login" ,
53
72
} ) . then ( ( ) => {
54
73
if ( ! mounted ) return ;
55
74
router . push ( "/login" ) ;
@@ -60,25 +79,37 @@ const Register = () => {
60
79
61
80
return (
62
81
< >
63
- < Navbar />
82
+ < Navbar />
64
83
< div className = "max-w-xl mx-auto my-10 p-2" >
65
84
< h1 className = "text-white font-extrabold text-h1" > Register</ h1 >
66
85
< p className = "text-primary-300 text-lg" >
67
- Register to our platform to access its features! Have an account? < a href = "/login" className = "text-yellow-500 hover:underline" > Login here!</ a >
86
+ Register to our platform to access its features! Have an account?{ " " }
87
+ < a href = "/login" className = "text-yellow-500 hover:underline" >
88
+ Login here!
89
+ </ a >
68
90
</ p >
69
91
< Form { ...form } >
70
- < form className = "my-10 grid gap-4" onSubmit = { form . handleSubmit ( onSubmit ) } >
92
+ < form
93
+ className = "my-10 grid gap-4"
94
+ onSubmit = { form . handleSubmit ( onSubmit ) }
95
+ >
71
96
< FormField
72
97
control = { form . control }
73
98
name = "username"
74
99
render = { ( { field } ) => (
75
100
< FormItem >
76
- < FormLabel className = "text-yellow-500 text-lg" > USERNAME</ FormLabel >
101
+ < FormLabel className = "text-yellow-500 text-lg" >
102
+ USERNAME
103
+ </ FormLabel >
77
104
< FormControl >
78
- < Input placeholder = "username" { ...field } className = "focus:border-yellow-500 text-white" />
105
+ < Input
106
+ placeholder = "username"
107
+ { ...field }
108
+ className = "focus:border-yellow-500 text-white"
109
+ />
79
110
</ FormControl >
80
111
{ /* <FormDescription>This is your public display name.</FormDescription> */ }
81
- < FormMessage />
112
+ < FormMessage />
82
113
</ FormItem >
83
114
) }
84
115
/>
@@ -87,12 +118,18 @@ const Register = () => {
87
118
name = "email"
88
119
render = { ( { field } ) => (
89
120
< FormItem >
90
- < FormLabel className = "text-yellow-500 text-lg" > EMAIL</ FormLabel >
121
+ < FormLabel className = "text-yellow-500 text-lg" >
122
+ EMAIL
123
+ </ FormLabel >
91
124
< FormControl >
92
- < Input placeholder = "email" { ...field } className = "focus:border-yellow-500 text-white" />
125
+ < Input
126
+ placeholder = "email"
127
+ { ...field }
128
+ className = "focus:border-yellow-500 text-white"
129
+ />
93
130
</ FormControl >
94
131
{ /* <FormDescription>This is your public display name.</FormDescription> */ }
95
- < FormMessage />
132
+ < FormMessage />
96
133
</ FormItem >
97
134
) }
98
135
/>
@@ -101,21 +138,33 @@ const Register = () => {
101
138
name = "password"
102
139
render = { ( { field } ) => (
103
140
< FormItem >
104
- < FormLabel className = "text-yellow-500 text-lg" > PASSWORD</ FormLabel >
141
+ < FormLabel className = "text-yellow-500 text-lg" >
142
+ PASSWORD
143
+ </ FormLabel >
105
144
< FormControl >
106
- < Input placeholder = "password" type = "password" { ...field } className = "focus:border-yellow-500 text-white" />
145
+ < Input
146
+ placeholder = "password"
147
+ type = "password"
148
+ { ...field }
149
+ className = "focus:border-yellow-500 text-white"
150
+ />
107
151
</ FormControl >
108
152
{ /* <FormDescription>This is your public display name.</FormDescription> */ }
109
- < FormMessage />
153
+ < FormMessage />
110
154
</ FormItem >
111
155
) }
112
156
/>
113
- < Button type = "submit" className = "bg-yellow-500 hover:bg-yellow-300 px-4 py-2 my-2 rounded-md text-black" > Register</ Button >
157
+ < Button
158
+ type = "submit"
159
+ className = "bg-yellow-500 hover:bg-yellow-300 px-4 py-2 my-2 rounded-md text-black"
160
+ >
161
+ Register
162
+ </ Button >
114
163
</ form >
115
164
</ Form >
116
165
</ div >
117
166
</ >
118
167
) ;
119
168
} ;
120
169
121
- export default Register ;
170
+ export default Register ;
0 commit comments