|
@@ -1,11 +1,14 @@
|
1
|
1
|
import * as React from 'react';
|
2
|
2
|
import toast, { Toaster } from 'react-hot-toast';
|
3
|
3
|
import jwt_decode from "jwt-decode";
|
4
|
|
-
|
5
|
|
-import {
|
6
|
|
- Paper, Box, Grid, Checkbox, FormControlLabel, Typography,
|
7
|
|
- TextField, CssBaseline, Button, Avatar,
|
8
|
|
- Backdrop, CircularProgress,
|
|
4
|
+import { useDispatch, useSelector } from 'react-redux';
|
|
5
|
+import { setToken } from '../Slices/tokenSlice'
|
|
6
|
+import { setRole, setProfile } from '../Slices/userSlice'
|
|
7
|
+
|
|
8
|
+import {
|
|
9
|
+ Paper, Box, Grid, Checkbox, FormControlLabel, Typography,
|
|
10
|
+ TextField, CssBaseline, Button, Avatar,
|
|
11
|
+ Backdrop, CircularProgress,
|
9
|
12
|
} from '@mui/material'
|
10
|
13
|
|
11
|
14
|
import { createTheme, ThemeProvider } from '@mui/material/styles';
|
|
@@ -14,7 +17,6 @@ import PersonIcon from '@mui/icons-material/Person';
|
14
|
17
|
import { useNavigate } from 'react-router-dom'
|
15
|
18
|
import { Copyright } from '../Components/Footer.js'
|
16
|
19
|
import { Link } from 'react-router-dom'
|
17
|
|
-import useAuth from '../Auth/useAuth';
|
18
|
20
|
|
19
|
21
|
import { useFormik } from 'formik';
|
20
|
22
|
import * as Yup from 'yup';
|
|
@@ -22,11 +24,11 @@ import * as Yup from 'yup';
|
22
|
24
|
import { Service } from '../Utils/HTTP.js'
|
23
|
25
|
|
24
|
26
|
const LoginSchema = Yup.object().shape({
|
25
|
|
- email : Yup
|
|
27
|
+ email: Yup
|
26
|
28
|
.string()
|
27
|
29
|
.email('El correo debe ser válido')
|
28
|
30
|
.required('El correo o usuario es requerido'),
|
29
|
|
- password : Yup
|
|
31
|
+ password: Yup
|
30
|
32
|
.string()
|
31
|
33
|
.required('El campo contraseña es requerido')
|
32
|
34
|
.min(5, 'La contraseña debe contener mínimo 5 caracteres')
|
|
@@ -36,177 +38,173 @@ const theme = createTheme();
|
36
|
38
|
|
37
|
39
|
export function Login() {
|
38
|
40
|
|
39
|
|
- let auth = useAuth();
|
40
|
|
- let navigate = useNavigate()
|
41
|
|
-
|
42
|
|
- const [open, setOpen] = React.useState(false);
|
43
|
|
- const handleClose = () => false
|
44
|
|
-
|
45
|
|
- const formik = useFormik({
|
46
|
|
- initialValues: {
|
47
|
|
- email: '',
|
48
|
|
- password: '',
|
49
|
|
- },
|
50
|
|
- validationSchema: LoginSchema,
|
51
|
|
- onSubmit: async (values) => {
|
52
|
|
-
|
53
|
|
- let { email, password } = values
|
54
|
|
- setOpen(true)
|
55
|
|
- let request = new Service(`/user?user=${email}&password=${password}&typeusr=isreclutador`)
|
56
|
|
- request
|
57
|
|
- .post({})
|
58
|
|
- .then( response => {
|
59
|
|
-
|
60
|
|
- console.log("Service Response :: ", response)
|
61
|
|
- let { token, nombre, apelidos, empresa } = response;
|
62
|
|
- toast.success(`Bienvenido ${nombre} ${apelidos}!!`)
|
63
|
|
- token = token.replace("Bearer ", "")
|
64
|
|
- console.log(token);
|
65
|
|
-
|
66
|
|
- let body_token = jwt_decode(token);
|
67
|
|
- console.log('',body_token)
|
68
|
|
- // let timestamp = body_token.exp * 1000;
|
69
|
|
- // let restante = timestamp - Date.now();
|
70
|
|
-
|
71
|
|
- // setTimeout(() => alert("Token Expirado") , restante )
|
72
|
|
- auth.setProfile(empresa)
|
73
|
|
- auth.setRole(body_token)
|
74
|
|
-
|
75
|
|
- setTimeout( () => {
|
76
|
|
- setOpen(false)
|
77
|
|
- auth.login(token)
|
78
|
|
- }, 2000)
|
79
|
|
-
|
80
|
|
- })
|
81
|
|
- .catch( err => {
|
82
|
|
- setOpen(false)
|
83
|
|
- toast.error("Ups! usuario o contraseña incorrectos")
|
84
|
|
- console.log("ERROR ", err)
|
85
|
|
- })
|
86
|
|
-
|
87
|
|
- },
|
88
|
|
- });
|
89
|
|
-
|
90
|
|
-
|
91
|
|
- React.useEffect(() => {
|
92
|
|
- if(auth.isLogged()){
|
93
|
|
- return navigate('/dashboard/home')
|
94
|
|
- }
|
95
|
|
- }, [auth,navigate])
|
96
|
|
-
|
97
|
|
-
|
98
|
|
- return (
|
99
|
|
- <ThemeProvider theme={theme}>
|
100
|
|
- <Grid container component="main" sx={{ height: '100vh' }}>
|
101
|
|
- <CssBaseline />
|
102
|
|
- <Grid
|
103
|
|
- item
|
104
|
|
- xs={false}
|
105
|
|
- sm={4}
|
106
|
|
- md={7}
|
107
|
|
- sx={{
|
108
|
|
- backgroundImage: 'url(https://source.unsplash.com/random)',
|
109
|
|
- backgroundRepeat: 'no-repeat',
|
110
|
|
- backgroundColor: (t) => t.palette.mode === 'light' ? t.palette.grey[50] : t.palette.grey[900],
|
111
|
|
- backgroundSize: 'cover',
|
112
|
|
- backgroundPosition: 'center',
|
113
|
|
- }}
|
114
|
|
- />
|
115
|
|
- <Grid item xs={12} sm={8} md={5} component={Paper} elevation={6} square>
|
116
|
|
-
|
117
|
|
- <Box
|
118
|
|
- sx={{
|
119
|
|
- my: 8,
|
120
|
|
- mx: 4,
|
121
|
|
- display: 'flex',
|
122
|
|
- flexDirection: 'column',
|
123
|
|
- alignItems: 'center',
|
124
|
|
- marginTop : '25%'
|
125
|
|
- }}>
|
126
|
|
- <Avatar sx={{ m: 1, bgcolor: '#fd4b4b' }}>
|
127
|
|
- <PersonIcon />
|
128
|
|
- </Avatar>
|
129
|
|
- <Typography component="h1" variant="h5">
|
130
|
|
- Ingresar
|
131
|
|
- </Typography>
|
132
|
|
-
|
133
|
|
- <Box component="form" noValidate onSubmit={formik.handleSubmit} sx={{ mt: 1 }}>
|
134
|
|
- <TextField
|
135
|
|
- value={formik.values.email}
|
136
|
|
- onChange={formik.handleChange}
|
137
|
|
- error={formik.touched.email && Boolean(formik.errors.email)}
|
138
|
|
- helperText={formik.touched.email && formik.errors.email}
|
139
|
|
- margin="normal"
|
140
|
|
- required
|
141
|
|
- fullWidth
|
142
|
|
- id="email"
|
143
|
|
- name="email"
|
144
|
|
- label="Correo Electrónico"
|
145
|
|
- autoComplete="email"
|
146
|
|
- autoFocus
|
147
|
|
- />
|
148
|
|
- <TextField
|
149
|
|
- value={formik.values.password}
|
150
|
|
- onChange={formik.handleChange}
|
151
|
|
- error={formik.touched.password && Boolean(formik.errors.password)}
|
152
|
|
- helperText={formik.touched.password && formik.errors.password}
|
153
|
|
- margin="normal"
|
154
|
|
- required
|
155
|
|
- fullWidth
|
156
|
|
- label="Contraseña"
|
157
|
|
- name="password"
|
158
|
|
- type="password"
|
159
|
|
- id="password"
|
160
|
|
- autoComplete="current-password"
|
161
|
|
- />
|
162
|
|
- <FormControlLabel
|
163
|
|
- control={<Checkbox value="remember" sx={{
|
164
|
|
- color: '#fd4b4b',
|
165
|
|
- '&.Mui-checked': {
|
166
|
|
- color: '#fd4b4b'
|
167
|
|
- },
|
168
|
|
- }}/>}
|
169
|
|
- label="Recordarme"
|
170
|
|
- />
|
171
|
|
- <Button
|
172
|
|
- id="login_btn_fn"
|
173
|
|
- type="submit"
|
174
|
|
- fullWidth
|
175
|
|
- variant="contained"
|
176
|
|
- sx={{ mt: 3, mb: 2, bgcolor :'var(--main)' }}
|
177
|
|
- >
|
178
|
|
- Ingresar
|
179
|
|
- </Button>
|
180
|
|
- <Grid container>
|
181
|
|
- <Grid item xs>
|
182
|
|
- <Link className="login_link" to='/password/recuperar'>
|
183
|
|
- ¿Olvidaste tu contraseña?
|
184
|
|
- </Link>
|
185
|
|
- </Grid>
|
186
|
|
- <Grid item>
|
187
|
|
- <Link className="login_link" to='/register'>
|
188
|
|
- {"¿No tienes cuenta? Regístrate"}
|
189
|
|
- </Link>
|
190
|
|
- </Grid>
|
191
|
|
- </Grid>
|
192
|
|
- <Copyright sx={{ mt: 5 }} />
|
193
|
|
- </Box>
|
194
|
|
- </Box>
|
|
41
|
+ const navigate = useNavigate()
|
|
42
|
+ const auth = useSelector((state) => state.token)
|
|
43
|
+ const dispatch = useDispatch();
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+ const [open, setOpen] = React.useState(false);
|
|
47
|
+ const handleClose = () => false
|
|
48
|
+
|
|
49
|
+ const formik = useFormik({
|
|
50
|
+ initialValues: {
|
|
51
|
+ email: '',
|
|
52
|
+ password: '',
|
|
53
|
+ },
|
|
54
|
+ validationSchema: LoginSchema,
|
|
55
|
+ onSubmit: async (values) => {
|
|
56
|
+
|
|
57
|
+ let { email, password } = values
|
|
58
|
+ setOpen(true)
|
|
59
|
+ let request = new Service(`/user?user=${email}&password=${password}&typeusr=isreclutador`)
|
|
60
|
+ request
|
|
61
|
+ .post({})
|
|
62
|
+ .then(response => {
|
|
63
|
+
|
|
64
|
+ let { token, nombre, apelidos, empresa } = response;
|
|
65
|
+ toast.success(`Bienvenido ${nombre} ${apelidos} !!`)
|
|
66
|
+ token = token.replace("Bearer ", "")
|
|
67
|
+ let body_token = jwt_decode(token);
|
|
68
|
+ let timestamp = body_token.exp * 1000;
|
|
69
|
+ let restante = timestamp - Date.now();
|
|
70
|
+
|
|
71
|
+ setTimeout(() => alert("Token Expirado") , restante )
|
|
72
|
+ dispatch(setProfile(empresa))
|
|
73
|
+ dispatch(setRole(body_token))
|
|
74
|
+
|
|
75
|
+ setTimeout(() => {
|
|
76
|
+ setOpen(false)
|
|
77
|
+ dispatch(setToken(token))
|
|
78
|
+ // auth.login(token)
|
|
79
|
+ }, 2000)
|
|
80
|
+
|
|
81
|
+ })
|
|
82
|
+ .catch(err => {
|
|
83
|
+ setOpen(false)
|
|
84
|
+ toast.error("Ups! usuario o contraseña incorrectos")
|
|
85
|
+ })
|
|
86
|
+
|
|
87
|
+ },
|
|
88
|
+ });
|
|
89
|
+
|
|
90
|
+ React.useEffect(() => {
|
|
91
|
+ if(auth.token){
|
|
92
|
+ navigate('/dashboard/home')
|
|
93
|
+ }
|
|
94
|
+ }, [auth, navigate])
|
|
95
|
+
|
|
96
|
+ return (
|
|
97
|
+ <ThemeProvider theme={theme}>
|
|
98
|
+ <Grid container component="main" sx={{ height: '100vh' }}>
|
|
99
|
+ <CssBaseline />
|
|
100
|
+ <Grid
|
|
101
|
+ item
|
|
102
|
+ xs={false}
|
|
103
|
+ sm={4}
|
|
104
|
+ md={7}
|
|
105
|
+ sx={{
|
|
106
|
+ backgroundImage: 'url(https://source.unsplash.com/random)',
|
|
107
|
+ backgroundRepeat: 'no-repeat',
|
|
108
|
+ backgroundColor: (t) => t.palette.mode === 'light' ? t.palette.grey[50] : t.palette.grey[900],
|
|
109
|
+ backgroundSize: 'cover',
|
|
110
|
+ backgroundPosition: 'center',
|
|
111
|
+ }}
|
|
112
|
+ />
|
|
113
|
+ <Grid item xs={12} sm={8} md={5} component={Paper} elevation={6} square>
|
|
114
|
+
|
|
115
|
+ <Box
|
|
116
|
+ sx={{
|
|
117
|
+ my: 8,
|
|
118
|
+ mx: 4,
|
|
119
|
+ display: 'flex',
|
|
120
|
+ flexDirection: 'column',
|
|
121
|
+ alignItems: 'center',
|
|
122
|
+ marginTop: '25%'
|
|
123
|
+ }}>
|
|
124
|
+ <Avatar sx={{ m: 1, bgcolor: '#fd4b4b' }}>
|
|
125
|
+ <PersonIcon />
|
|
126
|
+ </Avatar>
|
|
127
|
+ <Typography component="h1" variant="h5">
|
|
128
|
+ Ingresar
|
|
129
|
+ </Typography>
|
|
130
|
+
|
|
131
|
+ <Box component="form" noValidate onSubmit={formik.handleSubmit} sx={{ mt: 1 }}>
|
|
132
|
+ <TextField
|
|
133
|
+ value={formik.values.email}
|
|
134
|
+ onChange={formik.handleChange}
|
|
135
|
+ error={formik.touched.email && Boolean(formik.errors.email)}
|
|
136
|
+ helperText={formik.touched.email && formik.errors.email}
|
|
137
|
+ margin="normal"
|
|
138
|
+ required
|
|
139
|
+ fullWidth
|
|
140
|
+ id="email"
|
|
141
|
+ name="email"
|
|
142
|
+ label="Correo Electrónico"
|
|
143
|
+ autoComplete="email"
|
|
144
|
+ autoFocus
|
|
145
|
+ />
|
|
146
|
+ <TextField
|
|
147
|
+ value={formik.values.password}
|
|
148
|
+ onChange={formik.handleChange}
|
|
149
|
+ error={formik.touched.password && Boolean(formik.errors.password)}
|
|
150
|
+ helperText={formik.touched.password && formik.errors.password}
|
|
151
|
+ margin="normal"
|
|
152
|
+ required
|
|
153
|
+ fullWidth
|
|
154
|
+ label="Contraseña"
|
|
155
|
+ name="password"
|
|
156
|
+ type="password"
|
|
157
|
+ id="password"
|
|
158
|
+ autoComplete="current-password"
|
|
159
|
+ />
|
|
160
|
+ <FormControlLabel
|
|
161
|
+ control={<Checkbox value="remember" sx={{
|
|
162
|
+ color: '#fd4b4b',
|
|
163
|
+ '&.Mui-checked': {
|
|
164
|
+ color: '#fd4b4b'
|
|
165
|
+ },
|
|
166
|
+ }} />}
|
|
167
|
+ label="Recordarme"
|
|
168
|
+ />
|
|
169
|
+ <Button
|
|
170
|
+ id="login_btn_fn"
|
|
171
|
+ type="submit"
|
|
172
|
+ fullWidth
|
|
173
|
+ variant="contained"
|
|
174
|
+ sx={{ mt: 3, mb: 2, bgcolor: 'var(--main)' }}
|
|
175
|
+ >
|
|
176
|
+ Ingresar
|
|
177
|
+ </Button>
|
|
178
|
+ <Grid container>
|
|
179
|
+ <Grid item xs>
|
|
180
|
+ <Link className="login_link" to='/password/recuperar'>
|
|
181
|
+ ¿Olvidaste tu contraseña?
|
|
182
|
+ </Link>
|
|
183
|
+ </Grid>
|
|
184
|
+ <Grid item>
|
|
185
|
+ <Link className="login_link" to='/register'>
|
|
186
|
+ {"¿No tienes cuenta? Regístrate"}
|
|
187
|
+ </Link>
|
195
|
188
|
</Grid>
|
196
|
|
- </Grid>
|
197
|
|
- <Toaster
|
198
|
|
- position="top-left"
|
199
|
|
- reverseOrder={false}
|
200
|
|
- />
|
201
|
|
- <Backdrop
|
202
|
|
- sx={{ color: '#fd4b4b', zIndex: (theme) => theme.zIndex.drawer + 1 }}
|
203
|
|
- open={open}
|
204
|
|
- onClick={handleClose}
|
205
|
|
- >
|
206
|
|
- <CircularProgress color="inherit" />
|
207
|
|
- </Backdrop>
|
208
|
|
-
|
209
|
|
- </ThemeProvider>
|
210
|
|
-
|
211
|
|
- );
|
|
189
|
+ </Grid>
|
|
190
|
+ <Copyright sx={{ mt: 5 }} />
|
|
191
|
+ </Box>
|
|
192
|
+ </Box>
|
|
193
|
+ </Grid>
|
|
194
|
+ </Grid>
|
|
195
|
+ <Toaster
|
|
196
|
+ position="top-left"
|
|
197
|
+ reverseOrder={false}
|
|
198
|
+ />
|
|
199
|
+ <Backdrop
|
|
200
|
+ sx={{ color: '#fd4b4b', zIndex: (theme) => theme.zIndex.drawer + 1 }}
|
|
201
|
+ open={open}
|
|
202
|
+ onClick={handleClose}
|
|
203
|
+ >
|
|
204
|
+ <CircularProgress color="inherit" />
|
|
205
|
+ </Backdrop>
|
|
206
|
+
|
|
207
|
+ </ThemeProvider>
|
|
208
|
+
|
|
209
|
+ );
|
212
|
210
|
}
|