@@ -12,59 +12,79 @@ import Link from "next/link";
12
12
import { z } from "zod" ;
13
13
import { zodResolver } from "@hookform/resolvers/zod" ;
14
14
import { confirmResetPassword , verifyCode } from "@/services/authService" ;
15
- import { useRouter } from "next/navigation" ;
15
+ import { useRouter , useSearchParams } from "next/navigation" ;
16
16
import { useToast } from "@/hooks/use-toast" ;
17
17
import { TextInput } from "@/components/form/TextInput" ;
18
18
import { LoadingSpinner } from "@/components/LoadingSpinner" ;
19
19
import { Button } from "@/components/ui/button" ;
20
20
import { Form } from "@/components/ui/form" ;
21
21
import { Lock } from "lucide-react" ;
22
+ import { useCallback } from "react" ;
22
23
23
-
24
- const FormSchema = z . object ( {
25
- newPassword : z . string ( ) . min ( 6 , "Password must be at least 6 characters" ) ,
26
- confirmPassword : z . string ( ) . min ( 6 , "Password must be at least 6 characters" ) ,
27
- } ) . refine ( data => data . newPassword === data . confirmPassword , {
28
- message : "Passwords do not match" ,
29
- path : [ "confirmPassword" ] ,
30
- } ) ;
24
+ const FormSchema = z
25
+ . object ( {
26
+ newPassword : z . string ( ) . min ( 6 , "Password must be at least 6 characters" ) ,
27
+ confirmPassword : z
28
+ . string ( )
29
+ . min ( 6 , "Password must be at least 6 characters" ) ,
30
+ } )
31
+ . refine ( ( data ) => data . newPassword === data . confirmPassword , {
32
+ message : "Passwords do not match" ,
33
+ path : [ "confirmPassword" ] ,
34
+ } ) ;
31
35
32
36
export default function ResetPassword ( ) {
33
37
const router = useRouter ( ) ;
38
+ const searchParams = useSearchParams ( ) ;
34
39
const { toast } = useToast ( ) ;
40
+
35
41
const methods = useForm < z . infer < typeof FormSchema > > ( {
36
42
resolver : zodResolver ( FormSchema ) ,
37
43
} ) ;
38
44
const { handleSubmit, formState } = methods ;
39
45
40
- const { searchParams } = new URL ( window . location . href ) ;
41
46
const token = searchParams . get ( "token" ) ;
42
47
43
- const onSubmit = async ( data : z . infer < typeof FormSchema > ) => {
44
- if ( ! token ) {
45
- toast ( { title : "Error!" , description : "Invalid token." } ) ;
46
- return ;
47
- }
48
+ const onSubmit = useCallback (
49
+ async ( data : z . infer < typeof FormSchema > ) => {
50
+ if ( ! token ) {
51
+ toast ( { title : "Error!" , description : "Invalid token." } ) ;
52
+ return ;
53
+ }
48
54
49
- try {
50
- const verificationResponse = await verifyCode ( token ) ;
55
+ try {
56
+ const verificationResponse = await verifyCode ( token ) ;
51
57
52
- if ( verificationResponse . statusCode === 201 ) {
53
- const resetPasswordResponse = await confirmResetPassword ( token , data . newPassword ) ;
58
+ if ( verificationResponse . statusCode === 201 ) {
59
+ const resetPasswordResponse = await confirmResetPassword (
60
+ token ,
61
+ data . newPassword
62
+ ) ;
54
63
55
- if ( resetPasswordResponse . statusCode === 201 ) {
56
- toast ( { title : "Success!" , description : "Your password has been reset." } ) ;
57
- router . push ( '/forgotpassword/passwordupdated' ) ;
64
+ if ( resetPasswordResponse . statusCode === 201 ) {
65
+ toast ( {
66
+ title : "Success!" ,
67
+ description : "Your password has been reset." ,
68
+ } ) ;
69
+ router . push ( "/forgotpassword/passwordupdated" ) ;
70
+ } else {
71
+ toast ( {
72
+ title : "Error!" ,
73
+ description : resetPasswordResponse . message ,
74
+ } ) ;
75
+ }
58
76
} else {
59
- toast ( { title : "Error!" , description : resetPasswordResponse . message } ) ;
77
+ toast ( { title : "Error!" , description : verificationResponse . message } ) ;
60
78
}
61
- } else {
62
- toast ( { title : "Error!" , description : verificationResponse . message } ) ;
79
+ } catch ( error ) {
80
+ toast ( {
81
+ title : "Error!" ,
82
+ description : "An error occurred. Please try again." ,
83
+ } ) ;
63
84
}
64
- } catch ( error ) {
65
- toast ( { title : "Error!" , description : "An error occurred. Please try again." } ) ;
66
- }
67
- } ;
85
+ } ,
86
+ [ router , toast , token ]
87
+ ) ;
68
88
69
89
return (
70
90
< Card className = "p-2 mt-3" >
@@ -81,7 +101,7 @@ export default function ResetPassword() {
81
101
< div className = "flex flex-col gap-y-4" >
82
102
< TextInput
83
103
label = { "" }
84
- name = "newPassword"
104
+ name = "newPassword"
85
105
placeholder = "New Password"
86
106
type = "password"
87
107
Icon = { Lock }
@@ -101,18 +121,17 @@ export default function ResetPassword() {
101
121
disabled = { formState . isSubmitting }
102
122
className = "w-full py-2 mt-5 rounded-md bg-primary"
103
123
>
104
- { formState . isSubmitting ? (
105
- < LoadingSpinner />
106
- ) : (
107
- "Reset Password"
108
- ) }
124
+ { formState . isSubmitting ? < LoadingSpinner /> : "Reset Password" }
109
125
</ Button >
110
126
</ form >
111
127
</ Form >
112
128
< div className = "justify-center mt-6 text-sm text-center" >
113
129
< p >
114
130
Back to{ " " }
115
- < Link href = "/auth/signin" className = "hover:underline text-primary" >
131
+ < Link
132
+ href = "/auth/signin"
133
+ className = "hover:underline text-primary"
134
+ >
116
135
Sign in
117
136
</ Link >
118
137
</ p >
0 commit comments