@@ -4,7 +4,14 @@ import { checkPasswordResetCode, resetPasswordWithCode } from "@/api/user";
4
4
import LoadingPage from "@/app/common/LoadingPage" ;
5
5
import Navbar from "@/app/common/Navbar" ;
6
6
import { Button } from "@/components/ui/button" ;
7
- import { Form , FormControl , FormField , FormItem , FormLabel , FormMessage } from "@/components/ui/form" ;
7
+ import {
8
+ Form ,
9
+ FormControl ,
10
+ FormField ,
11
+ FormItem ,
12
+ FormLabel ,
13
+ FormMessage ,
14
+ } from "@/components/ui/form" ;
8
15
import { Input } from "@/components/ui/input" ;
9
16
import { zodResolver } from "@hookform/resolvers/zod" ;
10
17
import { useSearchParams } from "next/navigation" ;
@@ -13,9 +20,17 @@ import { useForm } from "react-hook-form";
13
20
import { z } from "zod" ;
14
21
15
22
const formSchema = z . object ( {
16
- password : z . string ( )
23
+ password : z
24
+ . string ( )
17
25
. min ( 8 , "Password must be at least 8 characters" )
18
- . max ( 100 , "Password must be at most 100 characters" ) ,
26
+ . max ( 100 , "Password must be at most 100 characters" )
27
+ . regex ( / [ a - z ] / , "Password must contain at least one lowercase letter" )
28
+ . regex ( / [ A - Z ] / , "Password must contain at least one uppercase letter" )
29
+ . regex ( / \d / , "Password must contain at least one number" )
30
+ . regex (
31
+ / [ @ $ ! % * ? & ] / ,
32
+ "Password must contain at least one special character (@, $, !, %, *, ?, &)"
33
+ ) ,
19
34
} ) ;
20
35
21
36
const ResetPassword = ( ) => {
@@ -44,8 +59,8 @@ const ResetPassword = () => {
44
59
}
45
60
setIsValidCode ( true ) ;
46
61
setUsername ( data . username ) ;
47
- } )
48
- } , [ searchParams ] )
62
+ } ) ;
63
+ } , [ searchParams ] ) ;
49
64
50
65
const onSubmit = async ( data : z . infer < typeof formSchema > ) => {
51
66
if ( ! searchParams ) return ;
@@ -55,52 +70,81 @@ const ResetPassword = () => {
55
70
resetPasswordWithCode ( code , data . password ) . then ( ( data ) => {
56
71
if ( ! data ) return ;
57
72
setChangeIsSuccessful ( true ) ;
58
- } )
73
+ } ) ;
59
74
} ;
60
75
61
76
return (
62
77
< >
63
- < Navbar />
64
- { ! isValidCode && ! isInvalidCode && < LoadingPage /> }
65
- { isValidCode && ! changeIsSuccessful && < div className = "max-w-xl mx-auto my-10 p-2" >
66
- < h1 className = "text-white font-extrabold text-h1" > Reset Password</ h1 >
67
- < p className = "text-primary-300 text-lg" >
68
- Welcome, { username } ! Please enter your new password.
69
- </ p >
70
- < Form { ...form } >
71
- < form className = "my-10 grid gap-4" onSubmit = { form . handleSubmit ( onSubmit ) } noValidate >
72
- < FormField
73
- control = { form . control }
74
- name = "password"
75
- render = { ( { field } ) => (
76
- < FormItem >
77
- < FormLabel className = "text-yellow-500 text-lg" > PASSWORD</ FormLabel >
78
- < FormControl >
79
- < Input placeholder = "password" type = "password" { ...field } className = "focus:border-yellow-500 text-white" />
80
- </ FormControl >
81
- { /* <FormDescription>This is your public display name.</FormDescription> */ }
82
- < FormMessage />
83
- </ FormItem >
84
- ) }
85
- />
86
- < Button type = "submit" className = "bg-yellow-500 hover:bg-yellow-300 px-4 py-2 my-2 rounded-md text-black" > Change Password</ Button >
87
- </ form >
88
- </ Form >
89
- </ div > }
90
- { isInvalidCode && < div className = "max-w-xl mx-auto my-10 p-2" >
91
- < h1 className = "text-white font-extrabold text-h1" > Invalid Code</ h1 >
92
- < p className = "text-primary-300 text-lg" >
93
- The code you entered is invalid. Please check your email for the correct code.
94
- </ p >
95
- </ div > }
96
- { changeIsSuccessful && < div className = "max-w-xl mx-auto my-10 p-2" >
97
- < h1 className = "text-white font-extrabold text-h1" > Password Changed</ h1 >
98
- < p className = "text-primary-300 text-lg" >
99
- Your password has been changed successfully. You can now < a href = "/login" className = "text-yellow-500 hover:underline" > login</ a > with your new password.
100
- </ p >
101
- </ div > }
78
+ < Navbar />
79
+ { ! isValidCode && ! isInvalidCode && < LoadingPage /> }
80
+ { isValidCode && ! changeIsSuccessful && (
81
+ < div className = "max-w-xl mx-auto my-10 p-2" >
82
+ < h1 className = "text-white font-extrabold text-h1" > Reset Password</ h1 >
83
+ < p className = "text-primary-300 text-lg" >
84
+ Welcome, { username } ! Please enter your new password.
85
+ </ p >
86
+ < Form { ...form } >
87
+ < form
88
+ className = "my-10 grid gap-4"
89
+ onSubmit = { form . handleSubmit ( onSubmit ) }
90
+ noValidate
91
+ >
92
+ < FormField
93
+ control = { form . control }
94
+ name = "password"
95
+ render = { ( { field } ) => (
96
+ < FormItem >
97
+ < FormLabel className = "text-yellow-500 text-lg" >
98
+ PASSWORD
99
+ </ FormLabel >
100
+ < FormControl >
101
+ < Input
102
+ placeholder = "password"
103
+ type = "password"
104
+ { ...field }
105
+ className = "focus:border-yellow-500 text-white"
106
+ />
107
+ </ FormControl >
108
+ { /* <FormDescription>This is your public display name.</FormDescription> */ }
109
+ < FormMessage />
110
+ </ FormItem >
111
+ ) }
112
+ />
113
+ < Button
114
+ type = "submit"
115
+ className = "bg-yellow-500 hover:bg-yellow-300 px-4 py-2 my-2 rounded-md text-black"
116
+ >
117
+ Change Password
118
+ </ Button >
119
+ </ form >
120
+ </ Form >
121
+ </ div >
122
+ ) }
123
+ { isInvalidCode && (
124
+ < div className = "max-w-xl mx-auto my-10 p-2" >
125
+ < h1 className = "text-white font-extrabold text-h1" > Invalid Code</ h1 >
126
+ < p className = "text-primary-300 text-lg" >
127
+ The code you entered is invalid. Please check your email for the
128
+ correct code.
129
+ </ p >
130
+ </ div >
131
+ ) }
132
+ { changeIsSuccessful && (
133
+ < div className = "max-w-xl mx-auto my-10 p-2" >
134
+ < h1 className = "text-white font-extrabold text-h1" >
135
+ Password Changed
136
+ </ h1 >
137
+ < p className = "text-primary-300 text-lg" >
138
+ Your password has been changed successfully. You can now{ " " }
139
+ < a href = "/login" className = "text-yellow-500 hover:underline" >
140
+ login
141
+ </ a > { " " }
142
+ with your new password.
143
+ </ p >
144
+ </ div >
145
+ ) }
102
146
</ >
103
147
) ;
104
- }
148
+ } ;
105
149
106
- export default ResetPassword ;
150
+ export default ResetPassword ;
0 commit comments