Browse Source

login validation schemea

amenpunk 3 years ago
parent
commit
76f3049ace
3 changed files with 61 additions and 39 deletions
  1. 14 3
      src/Components/Footer.js
  2. 45 35
      src/Pages/Login.jsx
  3. 2 1
      src/Pages/PruebaAsignar.jsx

+ 14 - 3
src/Components/Footer.js

1
-
2
-import Typography from '@mui/material/Typography';
3
-import Link from '@mui/material/Link';
1
+import { Typography, Link } from '@mui/material'
4
 
2
 
5
 export default function Footer(props) {
3
 export default function Footer(props) {
6
     return (
4
     return (
14
         </Typography>
12
         </Typography>
15
     );
13
     );
16
 }
14
 }
15
+
16
+export function Copyright(props){
17
+    return (
18
+        <Typography variant="body2" color="text.secondary" align="center" {...props}>
19
+            {'Copyright © '}
20
+            <Link color="inherit" href="https://mui.com/">
21
+                GrupoDIT
22
+            </Link>{' '}
23
+            {new Date().getFullYear()}
24
+            {'.'}
25
+        </Typography>
26
+    );
27
+}

+ 45 - 35
src/Pages/Login.jsx

1
 import * as React from 'react';
1
 import * as React from 'react';
2
-import Avatar from '@mui/material/Avatar';
3
-import Button from '@mui/material/Button';
4
-import CssBaseline from '@mui/material/CssBaseline';
5
-import TextField from '@mui/material/TextField';
6
 
2
 
7
-import PersonIcon from '@mui/icons-material/Person';
8
-import Typography from '@mui/material/Typography';
3
+import { 
4
+    Paper, Box, Grid,Link, Checkbox, FormControlLabel, Typography,
5
+    TextField, CssBaseline, Button, Avatar
6
+} from '@mui/material'
7
+
9
 import { createTheme, ThemeProvider } from '@mui/material/styles';
8
 import { createTheme, ThemeProvider } from '@mui/material/styles';
10
-import {Paper, Box, Grid,Link, Checkbox, FormControlLabel } from '@mui/material'
11
 
9
 
10
+import PersonIcon from '@mui/icons-material/Person';
12
 import { useNavigate } from 'react-router-dom'
11
 import { useNavigate } from 'react-router-dom'
12
+import { Copyright } from '../Components/Footer.js'
13
 import useAuth from '../Auth/useAuth';
13
 import useAuth from '../Auth/useAuth';
14
 
14
 
15
-function Copyright(props){
16
-    return (
17
-        <Typography variant="body2" color="text.secondary" align="center" {...props}>
18
-            {'Copyright © '}
19
-            <Link color="inherit" href="https://mui.com/">
20
-                GrupoDIT
21
-            </Link>{' '}
22
-            {new Date().getFullYear()}
23
-            {'.'}
24
-        </Typography>
25
-    );
26
-}
15
+import { useFormik } from 'formik';
16
+import * as Yup from 'yup';
17
+
18
+const LoginSchema = Yup.object().shape({
19
+    email : Yup
20
+    .string()
21
+    .email('El correo debe ser válido')
22
+    .required('El correo es requerido'),
23
+
24
+    password : Yup
25
+    .string()
26
+    .required('El campo contraseña es requerido')
27
+    .min(6, 'La contraseña debe contener mínimo 6 caracteres')
28
+})
27
 
29
 
28
 const theme = createTheme();
30
 const theme = createTheme();
29
 
31
 
32
     let auth = useAuth();
34
     let auth = useAuth();
33
     let navigate = useNavigate()
35
     let navigate = useNavigate()
34
 
36
 
35
-    const handleSubmit = (event) => {
36
-
37
-        event.preventDefault();
38
-        const data = new FormData(event.currentTarget);
37
+    const formik = useFormik({
38
+        initialValues: {
39
+            email: '',
40
+            password: '',
41
+        },
42
+        validationSchema: LoginSchema,
43
+        onSubmit: (values) => {
44
+            // alert(JSON.stringify(values, null, 2));
45
+            // return navigate('/dashboard/home')
46
+            auth.login(values)
47
+        },
48
+    });
39
 
49
 
40
-        let user = {
41
-            email: data.get('email'),
42
-            password: data.get('password'),
43
-        }
44
-
45
-        auth.login(user)
46
-        return navigate('/dashboard/home')
47
-    };
48
 
50
 
49
     React.useEffect(() => {
51
     React.useEffect(() => {
50
-        console.log(auth)
51
         if(auth.isLogged()){
52
         if(auth.isLogged()){
52
             return navigate('/dashboard/home')
53
             return navigate('/dashboard/home')
53
         }    
54
         }    
88
                         <Typography component="h1" variant="h5">
89
                         <Typography component="h1" variant="h5">
89
                             Ingresar
90
                             Ingresar
90
                         </Typography>
91
                         </Typography>
91
-                        <Box component="form" noValidate onSubmit={handleSubmit} sx={{ mt: 1 }}>
92
+
93
+                        <Box component="form" noValidate onSubmit={formik.handleSubmit} sx={{ mt: 1 }}>
92
                             <TextField
94
                             <TextField
95
+                                value={formik.values.email}
96
+                                onChange={formik.handleChange}
97
+                                error={formik.touched.email && Boolean(formik.errors.email)}
98
+                                helperText={formik.touched.email && formik.errors.email}
93
                                 margin="normal"
99
                                 margin="normal"
94
                                 required
100
                                 required
95
                                 fullWidth
101
                                 fullWidth
96
                                 id="email"
102
                                 id="email"
97
-                                label="Email Address"
98
                                 name="email"
103
                                 name="email"
104
+                                label="Correo Electrónico"
99
                                 autoComplete="email"
105
                                 autoComplete="email"
100
                                 autoFocus
106
                                 autoFocus
101
                             />
107
                             />
102
                             <TextField
108
                             <TextField
109
+                                value={formik.values.password}
110
+                                onChange={formik.handleChange}
111
+                                error={formik.touched.password && Boolean(formik.errors.password)}
112
+                                helperText={formik.touched.password && formik.errors.password}
103
                                 margin="normal"
113
                                 margin="normal"
104
                                 required
114
                                 required
105
                                 fullWidth
115
                                 fullWidth
116
+                                label="Contraseña"
106
                                 name="password"
117
                                 name="password"
107
-                                label="Password"
108
                                 type="password"
118
                                 type="password"
109
                                 id="password"
119
                                 id="password"
110
                                 autoComplete="current-password"
120
                                 autoComplete="current-password"

+ 2 - 1
src/Pages/PruebaAsignar.jsx

14
 
14
 
15
 function CardPrueba(props){
15
 function CardPrueba(props){
16
 
16
 
17
-    let id = ID.next().value * 2105981203;
17
+    let id = ( parseInt( ID.next().value ) * 210598120309218301923 );
18
+    console.log("ID >> ", id)
18
 
19
 
19
     return(
20
     return(
20
         <Col key={id} md="4">
21
         <Col key={id} md="4">