Quellcode durchsuchen

login validation schemea

amenpunk vor 3 Jahren
Ursprung
Commit
76f3049ace
3 geänderte Dateien mit 61 neuen und 39 gelöschten Zeilen
  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,6 +1,4 @@
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 3
 export default function Footer(props) {
6 4
     return (
@@ -14,3 +12,16 @@ export default function Footer(props) {
14 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,29 +1,31 @@
1 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 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 11
 import { useNavigate } from 'react-router-dom'
12
+import { Copyright } from '../Components/Footer.js'
13 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 30
 const theme = createTheme();
29 31
 
@@ -32,22 +34,21 @@ export function Login() {
32 34
     let auth = useAuth();
33 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 51
     React.useEffect(() => {
50
-        console.log(auth)
51 52
         if(auth.isLogged()){
52 53
             return navigate('/dashboard/home')
53 54
         }    
@@ -88,23 +89,32 @@ export function Login() {
88 89
                         <Typography component="h1" variant="h5">
89 90
                             Ingresar
90 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 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 99
                                 margin="normal"
94 100
                                 required
95 101
                                 fullWidth
96 102
                                 id="email"
97
-                                label="Email Address"
98 103
                                 name="email"
104
+                                label="Correo Electrónico"
99 105
                                 autoComplete="email"
100 106
                                 autoFocus
101 107
                             />
102 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 113
                                 margin="normal"
104 114
                                 required
105 115
                                 fullWidth
116
+                                label="Contraseña"
106 117
                                 name="password"
107
-                                label="Password"
108 118
                                 type="password"
109 119
                                 id="password"
110 120
                                 autoComplete="current-password"

+ 2 - 1
src/Pages/PruebaAsignar.jsx

@@ -14,7 +14,8 @@ var ID = idMaker();
14 14
 
15 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 20
     return(
20 21
         <Col key={id} md="4">