2630 |
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 { Lock } from '@mui/icons-material'
|
|
|
6 |
import Recaptcha from 'react-recaptcha'
|
|
|
7 |
|
|
|
8 |
import { axios } from '../../utils'
|
|
|
9 |
import { addNotification } from '../../redux/notification/notification.actions'
|
|
|
10 |
import CryptoJSAesJson from '../../utils/crypto-js/cryptojs-aes-format'
|
|
|
11 |
|
|
|
12 |
import Button from '@buttons'
|
|
|
13 |
import AuthForm from '../../components/auth/AuthForm'
|
|
|
14 |
import Input from 'components/UI/form/FormInputText'
|
|
|
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 { control, handleSubmit, errors, watch } = useForm({
|
|
|
26 |
mode: 'all'
|
|
|
27 |
})
|
|
|
28 |
|
|
|
29 |
const loginVerifyCallbackHandler = (response) => {
|
|
|
30 |
reCaptchaToken.current = response
|
|
|
31 |
setIsVerified(true)
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
const loginExpiredCallbackHandler = () => {
|
|
|
35 |
reCaptchaToken.current = ''
|
|
|
36 |
setIsVerified(false)
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
const handleOnRecaptchaLoad = () => {
|
|
|
40 |
reCaptchaToken.current = ''
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
const resetCaptcha = () => {
|
|
|
44 |
reCaptchaInstance.current?.reset()
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
const resetPassword = handleSubmit(async (data) => {
|
|
|
48 |
const formData = new FormData()
|
|
|
49 |
|
|
|
50 |
Object.entries(data).map(([key, value]) =>
|
|
|
51 |
(key === 'confirmation' || key === 'password') && value
|
|
|
52 |
? formData.append(key, CryptoJSAesJson.encrypt(value, aes))
|
|
|
53 |
: formData.append(key, value)
|
|
|
54 |
)
|
|
|
55 |
|
|
|
56 |
formData.append('captcha', reCaptchaToken.current)
|
|
|
57 |
|
|
|
58 |
try {
|
|
|
59 |
const { data: response } = await axios.post(
|
|
|
60 |
`/reset-password/${uuid}`,
|
|
|
61 |
formData
|
|
|
62 |
)
|
|
|
63 |
const { data, success } = response
|
|
|
64 |
|
|
|
65 |
if (!success) {
|
|
|
66 |
const errorMessage =
|
|
|
67 |
typeof data === 'string' ? data : data.confirmation[0]
|
|
|
68 |
throw new Error(errorMessage)
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
dispatch(addNotification({ style: 'success', msg: data }))
|
|
|
72 |
navigate('/signin')
|
|
|
73 |
} catch (error) {
|
|
|
74 |
console.log(error)
|
|
|
75 |
dispatch(addNotification({ style: 'danger', msg: error.message }))
|
|
|
76 |
} finally {
|
|
|
77 |
loginExpiredCallbackHandler()
|
|
|
78 |
resetCaptcha()
|
|
|
79 |
}
|
|
|
80 |
})
|
|
|
81 |
|
|
|
82 |
useEffect(() => {
|
|
|
83 |
resetCaptcha()
|
|
|
84 |
}, [])
|
|
|
85 |
|
|
|
86 |
return (
|
|
|
87 |
<AuthForm onSubmit={resetPassword}>
|
|
|
88 |
<h3>Reiniciar clave</h3>
|
|
|
89 |
|
|
|
90 |
<Input
|
|
|
91 |
type='password'
|
|
|
92 |
name='password'
|
|
|
93 |
icon={Lock}
|
|
|
94 |
placeholder='Clave'
|
|
|
95 |
error={errors.password?.message}
|
|
|
96 |
control={control}
|
|
|
97 |
rules={{
|
|
|
98 |
required: 'Este campo es requerido',
|
|
|
99 |
pattern: {
|
|
|
100 |
value:
|
|
|
101 |
/^(?=.*?[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) =>
|
|
|
118 |
v === watch('password') ||
|
|
|
119 |
'Disculpe, las claves tienen que coincidir'
|
|
|
120 |
}}
|
|
|
121 |
/>
|
|
|
122 |
|
|
|
123 |
<Recaptcha
|
|
|
124 |
sitekey={site_key}
|
|
|
125 |
verifyCallback={loginVerifyCallbackHandler}
|
|
|
126 |
verifyCallbackName='loginVerifyCallbackHandler'
|
|
|
127 |
expiredCallback={loginExpiredCallbackHandler}
|
|
|
128 |
expiredCallbackName='loginExpiredCallbackHandler'
|
|
|
129 |
ref={reCaptchaInstance}
|
|
|
130 |
render='explicit'
|
|
|
131 |
onloadCallback={handleOnRecaptchaLoad}
|
|
|
132 |
hl='es'
|
|
|
133 |
/>
|
|
|
134 |
|
|
|
135 |
<div className='links'>
|
|
|
136 |
<Link to='/signin'>Inicia sesión</Link>
|
|
|
137 |
<Link to='/signup'>¿No tienes cuenta?</Link>
|
|
|
138 |
</div>
|
|
|
139 |
|
|
|
140 |
<Button variant='secondary' type='submit' disabled={!isVerified}>
|
|
|
141 |
Guardar
|
|
|
142 |
</Button>
|
|
|
143 |
</AuthForm>
|
|
|
144 |
)
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
export default ResetPassword
|