|
@@ -4,9 +4,9 @@ import { useNavigate } from 'react-router-dom'
|
4
|
4
|
import jwt_decode from "jwt-decode";
|
5
|
5
|
|
6
|
6
|
import {
|
7
|
|
- Paper, Box, Grid, Typography,
|
8
|
|
- TextField, Button, Avatar,
|
9
|
|
- Backdrop, CircularProgress,
|
|
7
|
+ Paper, Box, Grid, Typography,
|
|
8
|
+ TextField, Button, Avatar,
|
|
9
|
+ Backdrop, CircularProgress,
|
10
|
10
|
} from '@mui/material'
|
11
|
11
|
|
12
|
12
|
import { createTheme, ThemeProvider } from '@mui/material/styles';
|
|
@@ -23,166 +23,170 @@ import * as Yup from 'yup';
|
23
|
23
|
import { Service } from '../Utils/HTTP.js'
|
24
|
24
|
|
25
|
25
|
const LoginSchema = Yup.object().shape({
|
26
|
|
- email: Yup
|
27
|
|
- .string()
|
28
|
|
- .email('El correo debe ser válido')
|
29
|
|
- .required('El correo o usuario es requerido'),
|
30
|
|
- password: Yup
|
31
|
|
- .string()
|
32
|
|
- .required('El campo contraseña es requerido')
|
33
|
|
- .min(5, 'La contraseña debe contener mínimo 5 caracteres')
|
|
26
|
+ email: Yup
|
|
27
|
+ .string()
|
|
28
|
+ .email('El correo debe ser válido')
|
|
29
|
+ .required('El correo o usuario es requerido'),
|
|
30
|
+ password: Yup
|
|
31
|
+ .string()
|
|
32
|
+ .required('El campo contraseña es requerido')
|
|
33
|
+ .min(5, 'La contraseña debe contener mínimo 5 caracteres')
|
34
|
34
|
})
|
35
|
35
|
|
36
|
36
|
const theme = createTheme();
|
37
|
37
|
|
38
|
38
|
export function LoginCs() {
|
39
|
39
|
|
40
|
|
- let auth = useAuth();
|
41
|
|
- let navigate = useNavigate()
|
42
|
|
-
|
43
|
|
- const [open, setOpen] = React.useState(false);
|
44
|
|
- const handleClose = () => false
|
45
|
|
-
|
46
|
|
- const formik = useFormik({
|
47
|
|
- initialValues: {
|
48
|
|
- email: '',
|
49
|
|
- password: '',
|
50
|
|
- },
|
51
|
|
- validationSchema: LoginSchema,
|
52
|
|
- onSubmit: async (values) => {
|
53
|
|
-
|
54
|
|
- //
|
55
|
|
- // QW5hbGlzdGEgZGUgY29tcHJhcw==
|
56
|
|
- //
|
57
|
|
-
|
58
|
|
- let { email, password } = values
|
59
|
|
- setOpen(true)
|
60
|
|
-
|
61
|
|
- let request = new Service(`/user?user=${email}&password=${password}&typeusr=iscandidato`)
|
62
|
|
- request
|
63
|
|
- .post({})
|
64
|
|
- .then(response => {
|
65
|
|
-
|
66
|
|
- console.log("Service Response :: ", response)
|
67
|
|
- let { token, nombre, apelidos, id : pass_id} = response;
|
68
|
|
- toast.success(`Bienvenido ${nombre} ${apelidos}!!`)
|
69
|
|
- token = token.replace("Bearer ", "")
|
70
|
|
- console.log(token);
|
71
|
|
-
|
72
|
|
- // let { exp } = jwt_decode(token);
|
73
|
|
- let body_token = jwt_decode(token);
|
74
|
|
- body_token['pass'] = password;
|
75
|
|
- body_token['passid'] = pass_id;
|
76
|
|
- console.log("BODY TOKEN: ", body_token)
|
77
|
|
- // let timestamp = exp * 1000;
|
78
|
|
- // let restante = timestamp - Date.now();
|
79
|
|
-
|
80
|
|
- // setTimeout(() => alert("Token Expirado"), restante)
|
81
|
|
- auth.setProfile({
|
82
|
|
- email, password
|
83
|
|
- })
|
84
|
|
- auth.setRole(body_token)
|
85
|
|
-
|
86
|
|
- setTimeout(() => {
|
87
|
|
- setOpen(false)
|
88
|
|
- auth.login(token)
|
89
|
|
- }, 2000)
|
90
|
|
-
|
91
|
|
- })
|
92
|
|
- .catch(err => {
|
93
|
|
- setOpen(false)
|
94
|
|
- toast.error("Ups! usuario o contraseña incorrectos")
|
95
|
|
- console.log("ERROR ", err)
|
96
|
|
- })
|
97
|
|
-
|
98
|
|
- },
|
99
|
|
- });
|
100
|
|
-
|
101
|
|
- React.useEffect(() => {
|
102
|
|
- if(auth.isLogged()){
|
103
|
|
- return navigate('/user/home')
|
104
|
|
- }
|
105
|
|
- }, [auth,navigate])
|
106
|
|
-
|
107
|
|
- return (
|
108
|
|
- <ThemeProvider theme={theme}>
|
109
|
|
- <Grid container component="main" sx={{ height: '100vh' }}>
|
110
|
|
- <Grid item xs={12} sm={12} md={12} component={Paper} elevation={6} square>
|
111
|
|
-
|
112
|
|
- <Box
|
113
|
|
- sx={{
|
114
|
|
- my: 8,
|
115
|
|
- mx: 4,
|
116
|
|
- marginTop:25,
|
117
|
|
- display: 'flex',
|
118
|
|
- flexDirection: 'column',
|
119
|
|
- // alignItems:true?'flex-start': 'center',
|
120
|
|
- alignItems:'center'
|
121
|
|
- }}
|
122
|
|
- >
|
123
|
|
- <Avatar sx={{ m: 1, bgcolor: 'var(--second)' }}>
|
124
|
|
- <PersonIcon />
|
125
|
|
- </Avatar>
|
126
|
|
- <Typography component="h1" variant="h5">
|
127
|
|
- Ingresar
|
128
|
|
- </Typography>
|
129
|
|
-
|
130
|
|
- <Box component="form" noValidate onSubmit={formik.handleSubmit} sx={{ mt: 1 }}>
|
131
|
|
- <TextField
|
132
|
|
- value={formik.values.email}
|
133
|
|
- onChange={formik.handleChange}
|
134
|
|
- error={formik.touched.email && Boolean(formik.errors.email)}
|
135
|
|
- helperText={formik.touched.email && formik.errors.email}
|
136
|
|
- margin="normal"
|
137
|
|
- required
|
138
|
|
- fullWidth
|
139
|
|
- id="email"
|
140
|
|
- name="email"
|
141
|
|
- label="Correo Electrónico"
|
142
|
|
- autoComplete="email"
|
143
|
|
- autoFocus
|
144
|
|
- />
|
145
|
|
- <TextField
|
146
|
|
- value={formik.values.password}
|
147
|
|
- onChange={formik.handleChange}
|
148
|
|
- error={formik.touched.password && Boolean(formik.errors.password)}
|
149
|
|
- helperText={formik.touched.password && formik.errors.password}
|
150
|
|
- margin="normal"
|
151
|
|
- required
|
152
|
|
- fullWidth
|
153
|
|
- label="Contraseña"
|
154
|
|
- name="password"
|
155
|
|
- type="password"
|
156
|
|
- id="password"
|
157
|
|
- autoComplete="current-password"
|
158
|
|
- />
|
159
|
|
- <Button
|
160
|
|
- id="login_btn_fn"
|
161
|
|
- type="submit"
|
162
|
|
- fullWidth
|
163
|
|
- variant="contained"
|
164
|
|
- sx={{ mt: 3, mb: 2, bgcolor: 'var(--second)' }}
|
165
|
|
- >
|
166
|
|
- Ingresar
|
167
|
|
- </Button>
|
168
|
|
- <Copyright sx={{ mt: 5 }} />
|
169
|
|
- </Box>
|
170
|
|
- </Box>
|
171
|
|
- </Grid>
|
172
|
|
- </Grid>
|
173
|
|
- <Toaster
|
174
|
|
- position="top-left"
|
175
|
|
- reverseOrder={false}
|
176
|
|
- />
|
177
|
|
- <Backdrop
|
178
|
|
- sx={{ color: '#fd4b4b', zIndex: (theme) => theme.zIndex.drawer + 1 }}
|
179
|
|
- open={open}
|
180
|
|
- onClick={handleClose}
|
181
|
|
- >
|
182
|
|
- <CircularProgress color="inherit" />
|
183
|
|
- </Backdrop>
|
184
|
|
-
|
185
|
|
- </ThemeProvider>
|
186
|
|
-
|
187
|
|
- );
|
|
40
|
+ let auth = useAuth();
|
|
41
|
+ let navigate = useNavigate()
|
|
42
|
+
|
|
43
|
+ const [open, setOpen] = React.useState(false);
|
|
44
|
+ const handleClose = () => false
|
|
45
|
+
|
|
46
|
+ const formik = useFormik({
|
|
47
|
+ initialValues: {
|
|
48
|
+ email: '',
|
|
49
|
+ password: '',
|
|
50
|
+ },
|
|
51
|
+ validationSchema: LoginSchema,
|
|
52
|
+ onSubmit: async (values) => {
|
|
53
|
+
|
|
54
|
+ //
|
|
55
|
+ // QW5hbGlzdGEgZGUgY29tcHJhcw==
|
|
56
|
+ //
|
|
57
|
+
|
|
58
|
+ let { email, password } = values
|
|
59
|
+ setOpen(true)
|
|
60
|
+
|
|
61
|
+ let request = new Service(`/user?user=${email}&password=${password}&typeusr=iscandidato`)
|
|
62
|
+ request
|
|
63
|
+ .post({})
|
|
64
|
+ .then(response => {
|
|
65
|
+
|
|
66
|
+ console.log("Service Response :: ", response)
|
|
67
|
+ let { token, nombre, apelidos, id: pass_id } = response;
|
|
68
|
+ toast.success(`Bienvenido ${nombre} ${apelidos}!!`)
|
|
69
|
+ token = token.replace("Bearer ", "")
|
|
70
|
+ console.log(token);
|
|
71
|
+
|
|
72
|
+ // let { exp } = jwt_decode(token);
|
|
73
|
+ let body_token = jwt_decode(token);
|
|
74
|
+ body_token['pass'] = password;
|
|
75
|
+ body_token['passid'] = pass_id;
|
|
76
|
+ console.log("BODY TOKEN: ", body_token)
|
|
77
|
+ // let timestamp = exp * 1000;
|
|
78
|
+ // let restante = timestamp - Date.now();
|
|
79
|
+
|
|
80
|
+ // setTimeout(() => alert("Token Expirado"), restante)
|
|
81
|
+ auth.setProfile({
|
|
82
|
+ email,
|
|
83
|
+ password,
|
|
84
|
+ passid: pass_id,
|
|
85
|
+ pass: password,
|
|
86
|
+ assingid: 'ok'
|
|
87
|
+ })
|
|
88
|
+ auth.setRole(body_token)
|
|
89
|
+
|
|
90
|
+ setTimeout(() => {
|
|
91
|
+ setOpen(false)
|
|
92
|
+ auth.login(token)
|
|
93
|
+ }, 2000)
|
|
94
|
+
|
|
95
|
+ })
|
|
96
|
+ .catch(err => {
|
|
97
|
+ setOpen(false)
|
|
98
|
+ toast.error("Ups! usuario o contraseña incorrectos")
|
|
99
|
+ console.log("ERROR ", err)
|
|
100
|
+ })
|
|
101
|
+
|
|
102
|
+ },
|
|
103
|
+ });
|
|
104
|
+
|
|
105
|
+ React.useEffect(() => {
|
|
106
|
+ if (auth.isLogged()) {
|
|
107
|
+ return navigate('/user/home')
|
|
108
|
+ }
|
|
109
|
+ }, [auth, navigate])
|
|
110
|
+
|
|
111
|
+ return (
|
|
112
|
+ <ThemeProvider theme={theme}>
|
|
113
|
+ <Grid container component="main" sx={{ height: '100vh' }}>
|
|
114
|
+ <Grid item xs={12} sm={12} md={12} component={Paper} elevation={6} square>
|
|
115
|
+
|
|
116
|
+ <Box
|
|
117
|
+ sx={{
|
|
118
|
+ my: 8,
|
|
119
|
+ mx: 4,
|
|
120
|
+ marginTop: 25,
|
|
121
|
+ display: 'flex',
|
|
122
|
+ flexDirection: 'column',
|
|
123
|
+ // alignItems:true?'flex-start': 'center',
|
|
124
|
+ alignItems: 'center'
|
|
125
|
+ }}
|
|
126
|
+ >
|
|
127
|
+ <Avatar sx={{ m: 1, bgcolor: 'var(--second)' }}>
|
|
128
|
+ <PersonIcon />
|
|
129
|
+ </Avatar>
|
|
130
|
+ <Typography component="h1" variant="h5">
|
|
131
|
+ Ingresar
|
|
132
|
+ </Typography>
|
|
133
|
+
|
|
134
|
+ <Box component="form" noValidate onSubmit={formik.handleSubmit} sx={{ mt: 1 }}>
|
|
135
|
+ <TextField
|
|
136
|
+ value={formik.values.email}
|
|
137
|
+ onChange={formik.handleChange}
|
|
138
|
+ error={formik.touched.email && Boolean(formik.errors.email)}
|
|
139
|
+ helperText={formik.touched.email && formik.errors.email}
|
|
140
|
+ margin="normal"
|
|
141
|
+ required
|
|
142
|
+ fullWidth
|
|
143
|
+ id="email"
|
|
144
|
+ name="email"
|
|
145
|
+ label="Correo Electrónico"
|
|
146
|
+ autoComplete="email"
|
|
147
|
+ autoFocus
|
|
148
|
+ />
|
|
149
|
+ <TextField
|
|
150
|
+ value={formik.values.password}
|
|
151
|
+ onChange={formik.handleChange}
|
|
152
|
+ error={formik.touched.password && Boolean(formik.errors.password)}
|
|
153
|
+ helperText={formik.touched.password && formik.errors.password}
|
|
154
|
+ margin="normal"
|
|
155
|
+ required
|
|
156
|
+ fullWidth
|
|
157
|
+ label="Contraseña"
|
|
158
|
+ name="password"
|
|
159
|
+ type="password"
|
|
160
|
+ id="password"
|
|
161
|
+ autoComplete="current-password"
|
|
162
|
+ />
|
|
163
|
+ <Button
|
|
164
|
+ id="login_btn_fn"
|
|
165
|
+ type="submit"
|
|
166
|
+ fullWidth
|
|
167
|
+ variant="contained"
|
|
168
|
+ sx={{ mt: 3, mb: 2, bgcolor: 'var(--second)' }}
|
|
169
|
+ >
|
|
170
|
+ Ingresar
|
|
171
|
+ </Button>
|
|
172
|
+ <Copyright sx={{ mt: 5 }} />
|
|
173
|
+ </Box>
|
|
174
|
+ </Box>
|
|
175
|
+ </Grid>
|
|
176
|
+ </Grid>
|
|
177
|
+ <Toaster
|
|
178
|
+ position="top-left"
|
|
179
|
+ reverseOrder={false}
|
|
180
|
+ />
|
|
181
|
+ <Backdrop
|
|
182
|
+ sx={{ color: '#fd4b4b', zIndex: (theme) => theme.zIndex.drawer + 1 }}
|
|
183
|
+ open={open}
|
|
184
|
+ onClick={handleClose}
|
|
185
|
+ >
|
|
186
|
+ <CircularProgress color="inherit" />
|
|
187
|
+ </Backdrop>
|
|
188
|
+
|
|
189
|
+ </ThemeProvider>
|
|
190
|
+
|
|
191
|
+ );
|
188
|
192
|
}
|