| 
 | 1 | +import React, { useState } from 'react';  | 
 | 2 | +import Image from 'next/image';  | 
 | 3 | +import logo from '../../../public/images/logo.png';  | 
 | 4 | +import Link from 'next/link';  | 
 | 5 | +import MiniFooter from '@/components/global/minifooter';  | 
 | 6 | +import {Amplify, Auth} from "aws-amplify";  | 
 | 7 | +import Spinner from '@/components/common/Spinner';  | 
 | 8 | +import { TextField } from '@mui/material';  | 
 | 9 | +import { isValidEmail, validateEmail, validatePassword } from '@/components/common/validation/validation';  | 
 | 10 | + | 
 | 11 | +const COGNITO_REGION = process.env.NEXT_PUBLIC_COGNITO_REGION;  | 
 | 12 | +const LOGIN_REGISTER_COGNITO_CLIENT_ID = process.env.NEXT_PUBLIC_LOGIN_REGISTER_COGNITO_CLIENT_ID;  | 
 | 13 | +const COGNITO_USER_POOL_ID = process.env.NEXT_PUBLIC_COGNITO_USER_POOL_ID;  | 
 | 14 | + | 
 | 15 | +const Recover = () => {  | 
 | 16 | +    const validationObject = {  | 
 | 17 | +        email: { error: false, fb: false, msg: '' },  | 
 | 18 | +        newPassword: { error: false, fb: false, msg: '' },  | 
 | 19 | +        confirmPassword: { error: false, fb: false, msg: '' },  | 
 | 20 | +        verificationCode: { error: false, fb: false, msg: '' },  | 
 | 21 | +    };  | 
 | 22 | +    const [isValid, setValidate] = useState(validationObject);  | 
 | 23 | +    const [email, setEmail] = useState('');  | 
 | 24 | +    const [activeConfirmCode, setActiveConfirmCode] = useState(false);  | 
 | 25 | +    const [successMessage, setSuccessMessage] = useState('');  | 
 | 26 | +    const [errorMessage, setErrorMessage] = useState('');  | 
 | 27 | +    const [loading, setLoading] = useState(false);  | 
 | 28 | +    const [user, setUser] = useState({  | 
 | 29 | +        verificationCode: '',  | 
 | 30 | +        newPassword: '',  | 
 | 31 | +        confirmPassword: '',  | 
 | 32 | +    });  | 
 | 33 | + | 
 | 34 | +    const awsAmplifyConfig = {  | 
 | 35 | +        mandatorySignId: true,  | 
 | 36 | +        region: COGNITO_REGION,  | 
 | 37 | +        userPoolId: COGNITO_USER_POOL_ID,  | 
 | 38 | +        userPoolWebClientId: LOGIN_REGISTER_COGNITO_CLIENT_ID  | 
 | 39 | +    };  | 
 | 40 | +    Amplify.configure(awsAmplifyConfig);  | 
 | 41 | + | 
 | 42 | +    const handleInputChange = (e: any) => {  | 
 | 43 | +        const { name, value } = e.target;  | 
 | 44 | +        setUser({ ...user, [name]: value });  | 
 | 45 | +    };  | 
 | 46 | + | 
 | 47 | +    const handleValidation = ({ target }: any) => {  | 
 | 48 | +        const { value, name, type } = target;  | 
 | 49 | +        const { newPassword } = user;  | 
 | 50 | +        let error = !value;  | 
 | 51 | +        let msg = '';  | 
 | 52 | +        if (!value) {  | 
 | 53 | +            msg = `${name} is required`;  | 
 | 54 | +        } else if (name === 'email') {  | 
 | 55 | +            msg = validateEmail(value);  | 
 | 56 | +            error = !isValidEmail(value);  | 
 | 57 | +        } else if (type === 'password') {  | 
 | 58 | +            msg = validatePassword(value);  | 
 | 59 | +            if(msg) error = true  | 
 | 60 | +            if (name === 'confirmPassword' &&  newPassword !== value)  {  | 
 | 61 | +                error = true  | 
 | 62 | +                msg ='Password does not match'  | 
 | 63 | +            }  | 
 | 64 | +        }  | 
 | 65 | +        setValidate({ ...isValid, [name]: { error, msg } });  | 
 | 66 | +    };  | 
 | 67 | + | 
 | 68 | +    const resetPassword = async (email: string) => {  | 
 | 69 | +        try {  | 
 | 70 | +            await Auth.forgotPassword(email);  | 
 | 71 | +            return { success: true };  | 
 | 72 | +        } catch (err: any) {  | 
 | 73 | +            console.error('Error requesting password reset:', err);  | 
 | 74 | +            return { success: false, error: err?.message };  | 
 | 75 | +        }  | 
 | 76 | +    };  | 
 | 77 | + | 
 | 78 | +    const handleResetPassword = async () => {  | 
 | 79 | +        setLoading(true);  | 
 | 80 | +        const resetResult = await resetPassword(email);  | 
 | 81 | +        setLoading(false);  | 
 | 82 | +        if (resetResult.success) {  | 
 | 83 | +            setSuccessMessage('Verification code sent to your email');  | 
 | 84 | +            setActiveConfirmCode(true);  | 
 | 85 | +        } else {  | 
 | 86 | +            setErrorMessage(resetResult.error || 'An unexpected error occurred');  | 
 | 87 | +        }  | 
 | 88 | +    };  | 
 | 89 | + | 
 | 90 | +    async function handleRest() {  | 
 | 91 | +        if(activeConfirmCode) {  | 
 | 92 | +                 await handleConfirmPwd()  | 
 | 93 | +        } else if (!email || validateEmail(email)) {  | 
 | 94 | +            setErrorMessage(isValid.email.msg);  | 
 | 95 | +        } else {  | 
 | 96 | +            setErrorMessage('');  | 
 | 97 | +          return  handleResetPassword()  | 
 | 98 | +        }  | 
 | 99 | +    }  | 
 | 100 | + | 
 | 101 | +    async function handleConfirmPwd() {  | 
 | 102 | +        const {verificationCode, confirmPassword, newPassword} = user;  | 
 | 103 | +        if(verificationCode && newPassword === confirmPassword ) {  | 
 | 104 | +            setLoading(true);  | 
 | 105 | +            try {  | 
 | 106 | +                await Auth.forgotPasswordSubmit(email, verificationCode, newPassword);  | 
 | 107 | +                setLoading(false);  | 
 | 108 | +                setActiveConfirmCode(false);  | 
 | 109 | +                setSuccessMessage('Password reset successfully');  | 
 | 110 | +            } catch (error) {  | 
 | 111 | +                console.error('Error resetting password:', error);  | 
 | 112 | +            }  | 
 | 113 | +        } else {  | 
 | 114 | +            setErrorMessage( 'Please enter valid data ');  | 
 | 115 | +        }  | 
 | 116 | +        setEmail('')  | 
 | 117 | +        setLoading(false);  | 
 | 118 | +    }  | 
 | 119 | + | 
 | 120 | +    return (  | 
 | 121 | +        <div className="main-box bg-dark-600" style={{ height: 'auto !important', minHeight: '100vh' }}>  | 
 | 122 | +            <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>  | 
 | 123 | +                <Image src={logo} alt="logo" className="mt-10 text-center" />  | 
 | 124 | +                <div className="whitebox bg-white rounded px-10 mt-11 py-5" style={{ width: '464px' }}>  | 
 | 125 | +                    <p className="text-black text-xl font-weight-bold mt-4 text-center" style={{ fontSize: '1.4rem' }}>  | 
 | 126 | +                        Reset Password  | 
 | 127 | +                    </p>  | 
 | 128 | +                    {errorMessage && (  | 
 | 129 | +                        <div className="font-regular mt-5 relative block w-full rounded-lg bg-red-500 p-4 text-base leading-5 text-white opacity-100">  | 
 | 130 | +                            {errorMessage}  | 
 | 131 | +                        </div>  | 
 | 132 | +                    )}  | 
 | 133 | +                    {successMessage && (  | 
 | 134 | +                        <div className="font-regular mt-5 relative block w-full rounded-lg bg-green-500 p-4 text-base leading-5 text-white opacity-100">  | 
 | 135 | +                            {successMessage}  | 
 | 136 | +                        </div>  | 
 | 137 | +                    )}  | 
 | 138 | +                    {loading && (  | 
 | 139 | +                        <div className={'align-items-center d-flex flex justify-center mt-3'}>  | 
 | 140 | +                            <Spinner height={'1rem'} width={'1rem'} />  | 
 | 141 | +                        </div>  | 
 | 142 | +                    )}  | 
 | 143 | +                    {!activeConfirmCode ? (  | 
 | 144 | +                        <TextField  | 
 | 145 | +                            label="Email Address"  | 
 | 146 | +                            id="emailAddress"  | 
 | 147 | +                            size="small"  | 
 | 148 | +                            name="email"  | 
 | 149 | +                            variant="standard"  | 
 | 150 | +                            className="w-full mb-6 mt-5"  | 
 | 151 | +                            value={email}  | 
 | 152 | +                            error={isValid['email']?.error}  | 
 | 153 | +                            helperText={isValid['email']?.msg}  | 
 | 154 | +                            onChange={(e: any) => setEmail(e.target.value)}  | 
 | 155 | +                            onBlur={handleValidation}  | 
 | 156 | +                        />  | 
 | 157 | +                    ) : (  | 
 | 158 | +                        <>  | 
 | 159 | +                            <TextField  | 
 | 160 | +                                label="Verification Code"  | 
 | 161 | +                                id="code"  | 
 | 162 | +                                size="small"  | 
 | 163 | +                                name="verificationCode"  | 
 | 164 | +                                variant="standard"  | 
 | 165 | +                                className="w-full mb-6 mt-5"  | 
 | 166 | +                                value={user.verificationCode}  | 
 | 167 | +                                error={isValid['verificationCode']?.error}  | 
 | 168 | +                                helperText={isValid['verificationCode']?.msg}  | 
 | 169 | +                                onChange={handleInputChange}  | 
 | 170 | +                                onBlur={handleValidation}  | 
 | 171 | +                            />  | 
 | 172 | +                            <TextField  | 
 | 173 | +                                label="New Password"  | 
 | 174 | +                                id="newPassword"  | 
 | 175 | +                                name="newPassword"  | 
 | 176 | +                                size="small"  | 
 | 177 | +                                variant="standard"  | 
 | 178 | +                                className="w-full mr-6 mb-6"  | 
 | 179 | +                                type="password"  | 
 | 180 | +                                value={user.newPassword}  | 
 | 181 | +                                onChange={handleInputChange}  | 
 | 182 | +                                onBlur={handleValidation}  | 
 | 183 | +                                error={isValid['newPassword']?.error}  | 
 | 184 | +                                helperText={isValid['newPassword']?.msg}  | 
 | 185 | +                            />  | 
 | 186 | +                            <TextField  | 
 | 187 | +                                label="Confirm Password"  | 
 | 188 | +                                id="confirmPassword"  | 
 | 189 | +                                name="confirmPassword"  | 
 | 190 | +                                size="small"  | 
 | 191 | +                                variant="standard"  | 
 | 192 | +                                type="password"  | 
 | 193 | +                                className="w-full mb-6"  | 
 | 194 | +                                onBlur={handleValidation}  | 
 | 195 | +                                value={user.confirmPassword}  | 
 | 196 | +                                onChange={handleInputChange}  | 
 | 197 | +                                error={isValid['confirmPassword']?.error}  | 
 | 198 | +                                helperText={isValid['confirmPassword']?.msg}  | 
 | 199 | +                            />  | 
 | 200 | +                        </>  | 
 | 201 | +                    )}  | 
 | 202 | +                        <button  | 
 | 203 | +                            onClick={handleRest}  | 
 | 204 | +                            className="text-white font-weight-bold text-center bg-dark-600 w-full rounded py-2 mt-5"  | 
 | 205 | +                        >  | 
 | 206 | +                            {!activeConfirmCode ? 'RESET PASSWORD' : ' Confirm'}  | 
 | 207 | +                        </button>  | 
 | 208 | +                    <Link href="/login">  | 
 | 209 | +                        <p className=" text-black text-xl font-weight-bold mt-5 text-center mb-5" style={{ color: '#1976D2',fontSize: '14px' }}>  | 
 | 210 | +                             Sign in  | 
 | 211 | +                        </p>  | 
 | 212 | +                    </Link>  | 
 | 213 | +                </div>  | 
 | 214 | +            </div>  | 
 | 215 | +            <MiniFooter />  | 
 | 216 | +        </div>  | 
 | 217 | +    );  | 
 | 218 | +};  | 
 | 219 | + | 
 | 220 | +export default Recover;  | 
0 commit comments