3719 |
stevensc |
1 |
import React from 'react';
|
|
|
2 |
import { useNavigate, Link } from 'react-router-dom';
|
|
|
3 |
import { useDispatch, useSelector } from 'react-redux';
|
|
|
4 |
import { Controller, useForm } from 'react-hook-form';
|
|
|
5 |
import { Button, Typography } from '@mui/material';
|
|
|
6 |
import Mail from '@mui/icons-material/Mail';
|
|
|
7 |
import Lock from '@mui/icons-material/Lock';
|
|
|
8 |
|
|
|
9 |
import { useMobile } from '@hooks';
|
|
|
10 |
import { asyncLogin } from '@store/auth/auth.actions';
|
|
|
11 |
import { addNotification } from '@store/notification/notification.actions';
|
|
|
12 |
import CryptoJSAesJson from '@utils/crypto-js/cryptojs-aes-format';
|
|
|
13 |
|
|
|
14 |
import Captcha from './captcha';
|
|
|
15 |
import Row from '@components/common/Row';
|
|
|
16 |
import Form from '@components/common/form';
|
|
|
17 |
import Input from '@components/UI/inputs/Input';
|
|
|
18 |
import CheckboxInput from '@components/UI/inputs/Checkbox';
|
|
|
19 |
import LoadingWrapper from '@components/common/loading-wrapper';
|
|
|
20 |
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
|
|
|
21 |
|
|
|
22 |
export default function LoginForm() {
|
|
|
23 |
const navigate = useNavigate();
|
|
|
24 |
const dispatch = useDispatch();
|
|
|
25 |
|
|
|
26 |
const { aes } = useSelector((state) => state.auth);
|
|
|
27 |
const isMobile = useMobile();
|
|
|
28 |
|
|
|
29 |
const {
|
|
|
30 |
formState: { errors, isSubmitting, isValid },
|
|
|
31 |
control,
|
|
|
32 |
handleSubmit,
|
|
|
33 |
reset
|
|
|
34 |
} = useForm({ mode: 'all' });
|
|
|
35 |
|
|
|
36 |
const onSubmitHandler = handleSubmit(async ({ email, password, remember, captcha }) => {
|
|
|
37 |
try {
|
|
|
38 |
const response = await dispatch(
|
|
|
39 |
asyncLogin({
|
|
|
40 |
email: CryptoJSAesJson.encrypt(email, aes),
|
|
|
41 |
password: CryptoJSAesJson.encrypt(password, aes),
|
|
|
42 |
captcha,
|
|
|
43 |
remember: remember ? 1 : 0
|
|
|
44 |
})
|
|
|
45 |
);
|
|
|
46 |
reset();
|
|
|
47 |
|
|
|
48 |
isMobile ? navigate('/apps-navigation') : navigate(response.redirect);
|
|
|
49 |
} catch (error) {
|
|
|
50 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
51 |
}
|
|
|
52 |
});
|
|
|
53 |
|
|
|
54 |
return (
|
|
|
55 |
<Form onSubmit={onSubmitHandler}>
|
|
|
56 |
<LoadingWrapper loading={isSubmitting} displayChildren></LoadingWrapper>
|
|
|
57 |
|
|
|
58 |
<Typography variant='h3'>Entrar</Typography>
|
|
|
59 |
|
|
|
60 |
<Input
|
|
|
61 |
type='email'
|
|
|
62 |
name='email'
|
|
|
63 |
icon={<Mail />}
|
|
|
64 |
placeholder='Correo electrónico'
|
|
|
65 |
rules={{
|
|
|
66 |
required: 'Este campo es requerido',
|
|
|
67 |
pattern: {
|
|
|
68 |
value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,}$/i,
|
|
|
69 |
message: 'Debe ser una dirección de correo electrónico valida'
|
|
|
70 |
},
|
|
|
71 |
maxLength: {
|
|
|
72 |
value: 64,
|
|
|
73 |
message: 'Debe ser menor a 64 caracteres'
|
|
|
74 |
}
|
|
|
75 |
}}
|
|
|
76 |
control={control}
|
|
|
77 |
error={errors.email?.message}
|
|
|
78 |
/>
|
|
|
79 |
|
|
|
80 |
<Input
|
|
|
81 |
type='password'
|
|
|
82 |
name='password'
|
|
|
83 |
icon={<Lock />}
|
|
|
84 |
placeholder='Clave'
|
|
|
85 |
control={control}
|
|
|
86 |
rules={{
|
|
|
87 |
required: 'Este campo es requerido',
|
|
|
88 |
pattern: {
|
|
|
89 |
value: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$/i,
|
|
|
90 |
message:
|
|
|
91 |
'Debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-'
|
|
|
92 |
}
|
|
|
93 |
}}
|
|
|
94 |
error={errors.password?.message}
|
|
|
95 |
/>
|
|
|
96 |
|
|
|
97 |
<CheckboxInput control={control} name='remember' label='Recuerdame' />
|
|
|
98 |
|
|
|
99 |
<Controller
|
|
|
100 |
name='captcha'
|
|
|
101 |
control={control}
|
|
|
102 |
render={({ field: { ref, onChange } }) => (
|
|
|
103 |
<>
|
|
|
104 |
<Captcha instanceRef={ref} onVerify={onChange} onExpired={onChange} />
|
|
|
105 |
{errors.captcha && <FormErrorFeedback>{errors.captcha.message}</FormErrorFeedback>}
|
|
|
106 |
</>
|
|
|
107 |
)}
|
|
|
108 |
/>
|
|
|
109 |
|
|
|
110 |
<Row>
|
|
|
111 |
<Link to='/forgot-password'>¿Has olvidado tu contraseña?</Link>
|
|
|
112 |
<Link to='/signup'>¿No tienes cuenta?</Link>
|
|
|
113 |
</Row>
|
|
|
114 |
|
|
|
115 |
<Button color='secondary' type='submit' disabled={!isValid}>
|
|
|
116 |
Entrar
|
|
|
117 |
</Button>
|
|
|
118 |
</Form>
|
|
|
119 |
);
|
|
|
120 |
}
|