3719 |
stevensc |
1 |
import React, { useEffect, useState, useRef } from 'react';
|
|
|
2 |
import { Link, useNavigate, useParams } from 'react-router-dom';
|
|
|
3 |
import { useForm } from 'react-hook-form';
|
|
|
4 |
import { useDispatch, useSelector } from 'react-redux';
|
|
|
5 |
import { Button, Typography } from '@mui/material';
|
|
|
6 |
import Lock from '@mui/icons-material/Lock';
|
|
|
7 |
import Recaptcha from 'react-recaptcha';
|
|
|
8 |
|
|
|
9 |
import { axios } from '@utils';
|
|
|
10 |
import CryptoJSAesJson from '@utils/crypto-js/cryptojs-aes-format';
|
|
|
11 |
import { addNotification } from '@store/notification/notification.actions';
|
|
|
12 |
|
|
|
13 |
import Form from '@components/common/form';
|
|
|
14 |
import Input from '@components/UI/inputs/Input';
|
|
|
15 |
|
|
|
16 |
const ResetPassword = () => {
|
|
|
17 |
const [isVerified, setIsVerified] = useState(false);
|
|
|
18 |
const reCaptchaToken = useRef('');
|
|
|
19 |
const reCaptchaInstance = useRef();
|
|
|
20 |
const dispatch = useDispatch();
|
|
|
21 |
const navigate = useNavigate();
|
|
|
22 |
const { uuid } = useParams();
|
|
|
23 |
const { site_key, aes } = useSelector(({ auth }) => auth);
|
|
|
24 |
|
|
|
25 |
const {
|
|
|
26 |
control,
|
|
|
27 |
handleSubmit,
|
|
|
28 |
watch,
|
|
|
29 |
formState: { errors }
|
|
|
30 |
} = useForm({
|
|
|
31 |
mode: 'all'
|
|
|
32 |
});
|
|
|
33 |
|
|
|
34 |
const loginVerifyCallbackHandler = (response) => {
|
|
|
35 |
reCaptchaToken.current = response;
|
|
|
36 |
setIsVerified(true);
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
const loginExpiredCallbackHandler = () => {
|
|
|
40 |
reCaptchaToken.current = '';
|
|
|
41 |
setIsVerified(false);
|
|
|
42 |
};
|
|
|
43 |
|
|
|
44 |
const handleOnRecaptchaLoad = () => {
|
|
|
45 |
reCaptchaToken.current = '';
|
|
|
46 |
};
|
|
|
47 |
|
|
|
48 |
const resetCaptcha = () => {
|
|
|
49 |
reCaptchaInstance.current?.reset();
|
|
|
50 |
};
|
|
|
51 |
|
|
|
52 |
const resetPassword = handleSubmit(async (data) => {
|
|
|
53 |
const formData = new FormData();
|
|
|
54 |
|
|
|
55 |
Object.entries(data).map(([key, value]) =>
|
|
|
56 |
(key === 'confirmation' || key === 'password') && value
|
|
|
57 |
? formData.append(key, CryptoJSAesJson.encrypt(value, aes))
|
|
|
58 |
: formData.append(key, value)
|
|
|
59 |
);
|
|
|
60 |
|
|
|
61 |
formData.append('captcha', reCaptchaToken.current);
|
|
|
62 |
|
|
|
63 |
try {
|
|
|
64 |
const response = await axios.post(`/reset-password/${uuid}`, formData);
|
|
|
65 |
const { data, success } = response.data;
|
|
|
66 |
|
|
|
67 |
if (!success) {
|
|
|
68 |
const errorMessage = typeof data === 'string' ? data : data.confirmation[0];
|
|
|
69 |
throw new Error(errorMessage);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
dispatch(addNotification({ style: 'success', msg: data }));
|
|
|
73 |
navigate('/signin');
|
|
|
74 |
} catch (error) {
|
|
|
75 |
console.log(error);
|
|
|
76 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
77 |
} finally {
|
|
|
78 |
loginExpiredCallbackHandler();
|
|
|
79 |
resetCaptcha();
|
|
|
80 |
}
|
|
|
81 |
});
|
|
|
82 |
|
|
|
83 |
useEffect(() => {
|
|
|
84 |
resetCaptcha();
|
|
|
85 |
}, []);
|
|
|
86 |
|
|
|
87 |
return (
|
|
|
88 |
<Form onSubmit={resetPassword}>
|
|
|
89 |
<Typography variant='h3'>Reiniciar clave</Typography>
|
|
|
90 |
|
|
|
91 |
<Input
|
|
|
92 |
type='password'
|
|
|
93 |
name='password'
|
|
|
94 |
icon={<Lock />}
|
|
|
95 |
placeholder='Clave'
|
|
|
96 |
error={errors.password?.message}
|
|
|
97 |
control={control}
|
|
|
98 |
rules={{
|
|
|
99 |
required: 'Este campo es requerido',
|
|
|
100 |
pattern: {
|
|
|
101 |
value: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$/i,
|
|
|
102 |
message:
|
|
|
103 |
'Debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-'
|
|
|
104 |
}
|
|
|
105 |
}}
|
|
|
106 |
/>
|
|
|
107 |
|
|
|
108 |
<Input
|
|
|
109 |
type='password'
|
|
|
110 |
name='confirmation'
|
|
|
111 |
icon={<Lock />}
|
|
|
112 |
placeholder='Repita nueva clave'
|
|
|
113 |
error={errors.confirmation?.message}
|
|
|
114 |
control={control}
|
|
|
115 |
rules={{
|
|
|
116 |
required: 'Este campo es requerido',
|
|
|
117 |
validate: (v) => v === watch('password') || 'Disculpe, las claves tienen que coincidir'
|
|
|
118 |
}}
|
|
|
119 |
/>
|
|
|
120 |
|
|
|
121 |
<Recaptcha
|
|
|
122 |
sitekey={site_key}
|
|
|
123 |
verifyCallback={loginVerifyCallbackHandler}
|
|
|
124 |
verifyCallbackName='loginVerifyCallbackHandler'
|
|
|
125 |
expiredCallback={loginExpiredCallbackHandler}
|
|
|
126 |
expiredCallbackName='loginExpiredCallbackHandler'
|
|
|
127 |
ref={reCaptchaInstance}
|
|
|
128 |
render='explicit'
|
|
|
129 |
onloadCallback={handleOnRecaptchaLoad}
|
|
|
130 |
hl='es'
|
|
|
131 |
/>
|
|
|
132 |
|
|
|
133 |
<div className='links'>
|
|
|
134 |
<Link to='/signin'>Inicia sesión</Link>
|
|
|
135 |
<Link to='/signup'>¿No tienes cuenta?</Link>
|
|
|
136 |
</div>
|
|
|
137 |
|
|
|
138 |
<Button color='primary' type='submit' disabled={!isVerified}>
|
|
|
139 |
Guardar
|
|
|
140 |
</Button>
|
|
|
141 |
</Form>
|
|
|
142 |
);
|
|
|
143 |
};
|
|
|
144 |
|
|
|
145 |
export default ResetPassword;
|