1+ import React from "react" ;
2+
3+ import { Button , Container , FormControl , FormErrorMessage , FormLabel , Heading , Input , Text } from "@chakra-ui/react" ;
4+ import { SubmitHandler , useForm } from "react-hook-form" ;
5+
6+ import { LoginService , NewPassword } from "../client" ;
7+ import useCustomToast from "../hooks/useCustomToast" ;
8+
9+ interface NewPasswordForm extends NewPassword {
10+ confirm_password : string ;
11+ }
12+
13+ const ResetPassword : React . FC = ( ) => {
14+ const { register, handleSubmit, getValues, formState : { errors } } = useForm < NewPasswordForm > ( {
15+ mode : 'onBlur' ,
16+ criteriaMode : 'all' ,
17+ defaultValues : {
18+ new_password : '' ,
19+ }
20+ } ) ;
21+ const showToast = useCustomToast ( ) ;
22+
23+ const onSubmit : SubmitHandler < NewPasswordForm > = async ( data ) => {
24+ try {
25+ const token = new URLSearchParams ( window . location . search ) . get ( 'token' ) ;
26+ await LoginService . resetPassword ( {
27+ requestBody : { new_password : data . new_password , token : token ! }
28+ } ) ;
29+ showToast ( "Password reset." , "Your password has been reset successfully." , "success" ) ;
30+ } catch ( error ) {
31+ showToast ( "Error" , "An error occurred while resetting your password." , "error" ) ;
32+ }
33+ } ;
34+
35+ return (
36+ < Container
37+ as = "form"
38+ onSubmit = { handleSubmit ( onSubmit ) }
39+ h = "100vh"
40+ maxW = "sm"
41+ alignItems = "stretch"
42+ justifyContent = "center"
43+ gap = { 4 }
44+ centerContent
45+ >
46+ < Heading size = "xl" color = "ui.main" textAlign = "center" mb = { 2 } >
47+ Reset Password
48+ </ Heading >
49+ < Text textAlign = "center" >
50+ Please enter your new password and confirm it to reset your password.
51+ </ Text >
52+ < FormControl mt = { 4 } isInvalid = { ! ! errors . new_password } >
53+ < FormLabel htmlFor = 'password' > Set Password</ FormLabel >
54+ < Input id = 'password' { ...register ( 'new_password' , { required : 'Password is required' , minLength : { value : 8 , message : 'Password must be at least 8 characters' } } ) } placeholder = 'Password' type = 'password' />
55+ { errors . new_password && < FormErrorMessage > { errors . new_password . message } </ FormErrorMessage > }
56+ </ FormControl >
57+ < FormControl mt = { 4 } isInvalid = { ! ! errors . confirm_password } >
58+ < FormLabel htmlFor = 'confirm_password' > Confirm Password</ FormLabel >
59+ < Input id = 'confirm_password' { ...register ( 'confirm_password' , {
60+ required : 'Please confirm your password' ,
61+ validate : value => value === getValues ( ) . new_password || 'The passwords do not match'
62+ } ) } placeholder = 'Password' type = 'password' />
63+ { errors . confirm_password && < FormErrorMessage > { errors . confirm_password . message } </ FormErrorMessage > }
64+ </ FormControl >
65+ < Button bg = "ui.main" color = "white" _hover = { { opacity : 0.8 } } type = "submit" >
66+ Reset Password
67+ </ Button >
68+ </ Container >
69+ ) ;
70+ } ;
71+
72+ export default ResetPassword ;
0 commit comments