Quellcode durchsuchen

[REF] custom private route

amenpunk vor 3 Jahren
Ursprung
Commit
0a181b31c9
7 geänderte Dateien mit 39411 neuen und 60 gelöschten Zeilen
  1. 39345 0
      package-lock.json
  2. 2 1
      package.json
  3. 1 1
      src/Auth/AuthProvider.js
  4. 1 1
      src/Components/Candidatos.js
  5. 46 29
      src/Components/Routes.js
  6. 13 25
      src/Components/listItems.js
  7. 3 3
      src/Pages/404.jsx

Datei-Diff unterdrückt, da er zu groß ist
+ 39345 - 0
package-lock.json


+ 2 - 1
package.json

@@ -20,7 +20,8 @@
20 20
     "react": "^17.0.2",
21 21
     "react-bootstrap": "^2.0.2",
22 22
     "react-dom": "^17.0.2",
23
-    "react-router-dom": "^6.0.2",
23
+    "react-router": "6.2.1",
24
+    "react-router-dom": "6.2.1",
24 25
     "react-scripts": "4.0.3",
25 26
     "web-vitals": "^1.0.1",
26 27
     "yup": "^0.32.11"

+ 1 - 1
src/Auth/AuthProvider.js

@@ -1,7 +1,7 @@
1 1
 import React from 'react'
2 2
 import Cookies from 'js-cookie'
3 3
 
4
-export const AuthContext = React.createContext();
4
+export const AuthContext = React.createContext(null);
5 5
 
6 6
 export function AuthProvider ({ children }){
7 7
 

+ 1 - 1
src/Components/Candidatos.js

@@ -23,7 +23,7 @@ export default function Candidatos () {
23 23
 
24 24
     const [page, setPage] = useState(1);
25 25
     const handleChange = ( _ , value) => {
26
-        console.log("INDEX => ", value)
26
+        console.log("INDEX => ", _ ,value)
27 27
         setPage(value);
28 28
     };
29 29
 

+ 46 - 29
src/Components/Routes.js

@@ -1,5 +1,5 @@
1 1
 import React, {useEffect} from 'react'
2
-import { Routes, Route, Navigate, useNavigate } from "react-router-dom";
2
+import { Routes, Route, Navigate, useNavigate, useLocation } from "react-router-dom";
3 3
 
4 4
 import { Dashboard } from "./Dashboard";
5 5
 import { Login } from '../Pages/Login'
@@ -18,45 +18,62 @@ import { Profile } from '../Pages/Profile'
18 18
 
19 19
 import useAuth from '../Auth/useAuth'
20 20
 
21
+export function RequireAuth({ children }) {
22
+    let auth = useAuth();
23
+    let location = useLocation();
24
+
25
+    console.log('requier auth >> ', auth)
26
+
27
+
28
+    if (!auth.isLogged()) {
29
+        // Redirect them to the /login page, but save the current location they were
30
+        // trying to go to when they were redirected. This allows us to send them
31
+        // along to that page after they login, which is a nicer user experience
32
+        // than dropping them off on the home page.
33
+        return <Navigate to="/login" state={{ from: location }} replace />;
34
+    }
35
+
36
+    return children;
37
+}
38
+
39
+
21 40
 export default function MyRoutes () {
22 41
 
23 42
     let auth = useAuth();
24 43
     let navigate = useNavigate()
25 44
 
26 45
     useEffect(() => {
27
-        if(!auth.isLogged(0)){
28
-            return navigate('/')
46
+        console.log('usse fec check user')
47
+        if(!auth.isLogged()){
48
+            return navigate('login')
49
+        }else{
50
+            return navigate('dashboard/home')
29 51
         }
30 52
     },[auth,navigate])
31
-    
32 53
 
33 54
     return(
34 55
         <Routes>
35
-
36
-            <Route path="/" element={auth.isLogged() ? <Navigate to='dashboard/home'/> : <Login/>}/>
37
-
38
-            {
39
-                auth.isLogged() ? 
40
-                    (
41
-                        <Route path="dashboard" element={<Dashboard/>}>
42
-                            <Route path="home" element={<Home/>} />
43
-                            <Route path="puestos" element={<Puestos/>} />
44
-                            <Route path="perfil" element={<Profile/>} />
45
-                            <Route path="contrasenas" element={<Contras/>} />
46
-                            <Route path="expedientes" element={<Expedientes/>} />
47
-                            <Route path="resultados" element={<Resultados/>} />
48
-                            <Route path="configuraciones" element={<Configuracion/>} />
49
-                            <Route path="historial" element={<Historial/>} />
50
-                            <Route path="pruebas/listar" element={<Pruebas/>} />
51
-                            <Route path="pruebas/crear" element={<PruebaNueva/>} />
52
-                            <Route path="pruebas/aplicar" element={<PruebaAsignar/>} />
53
-                        </Route>
54
-                    )
55
-                : ( <Route path="dashboard" element={<Navigate to='/'/>} />)
56
-            }
57
-
58
-
59
-
56
+            <Route path="/login" element={<Login/>} />
57
+            <Route 
58
+                path="dashboard" 
59
+                element={
60
+                    <RequireAuth>
61
+                        <Dashboard/>
62
+                    </RequireAuth>
63
+                }
64
+            >
65
+                <Route path="home" element={<Home/>} />
66
+                <Route path="puestos" element={<Puestos/>} />
67
+                <Route path="perfil" element={<Profile/>} />
68
+                <Route path="contrasenas" element={<Contras/>} />
69
+                <Route path="expedientes" element={<Expedientes/>} />
70
+                <Route path="resultados" element={<Resultados/>} />
71
+                <Route path="configuraciones" element={<Configuracion/>} />
72
+                <Route path="historial" element={<Historial/>} />
73
+                <Route path="pruebas/listar" element={<Pruebas/>} />
74
+                <Route path="pruebas/crear" element={<PruebaNueva/>} />
75
+                <Route path="pruebas/aplicar" element={<PruebaAsignar/>} />
76
+            </Route>
60 77
             <Route path="*" element={<NotFound/>}/>
61 78
 
62 79
         </Routes>

+ 13 - 25
src/Components/listItems.js

@@ -26,8 +26,7 @@ import Collapse from '@mui/material/Collapse';
26 26
 import ExpandLess from '@mui/icons-material/ExpandLess';
27 27
 import ExpandMore from '@mui/icons-material/ExpandMore';
28 28
 import { useNavigate } from 'react-router-dom'
29
-import useAuth from '../Auth/useAuth'
30
-    
29
+
31 30
 const SubMenuList = [5,6,7,8,9]
32 31
 
33 32
 function SubMenuItem (props) {
@@ -66,29 +65,22 @@ function SubMenuItem (props) {
66 65
 function Item (props) {
67 66
 
68 67
     let navigate = useNavigate()
69
-    let auth = useAuth()
70 68
 
71 69
     function change (event){
70
+        console.log('change >>> ', props)
72 71
         props.change(event, props.index)
73
-        // console.log('is logged', auth.isLogged())
74
-
75
-        if(!auth.isLogged()){
76
-            return navigate('/')
77
-        }
78
-
79 72
         navigate(props.route)
80 73
     } 
81 74
 
82 75
     let isOn = props.selected === props.index
83 76
 
84
-    //.css-10oikv1-MuiButtonBase-root-MuiListItem-root.Mui-selected
85 77
     return(
86 78
         <ListItem
87 79
             sx={{ color : '#25344f'}}
88 80
             selected={isOn}
89 81
             onClick={change} 
90 82
             button
91
-            >
83
+        >
92 84
             <ListItemIcon>
93 85
                 {props.icon}
94 86
             </ListItemIcon>
@@ -125,10 +117,8 @@ export const MainListItems = (props) =>  {
125 117
 
126 118
         if( SubMenuList.includes(selectedIndex) && !open ){
127 119
             if(props.AppBarVisible){
128
-                console.log('yes show')
129 120
                 return setOpen(false)
130 121
             }else{
131
-                console.log('not show')
132 122
                 return setOpen(true)
133 123
             }
134 124
         }
@@ -136,7 +126,7 @@ export const MainListItems = (props) =>  {
136 126
         if( !SubMenuList.includes(selectedIndex) && !props.AppBarVisible ){
137 127
             return setOpen(false)
138 128
         }
139
-        
129
+
140 130
 
141 131
     }, [props, selectedIndex, open])
142 132
 
@@ -155,23 +145,21 @@ export const MainListItems = (props) =>  {
155 145
             <Item icon={<VisibilityOffIcon/>} selected={selectedIndex}  change={handleListItemClick} index={2} title="Contraseñas" route="contrasenas" />
156 146
             <Item icon={<PeopleAltIcon/>} selected={selectedIndex}  change={handleListItemClick} index={3} title="Expedientes" route="expedientes" />
157 147
             <Item icon={<EqualizerIcon/>} selected={selectedIndex}  change={handleListItemClick} index={4} title="Resultados" route="resultados" />
158
-            {/* <Item icon={<FingerprintIcon/>} selected={selectedIndex}  change={handleListItemClick} index={5} title="Pruebas" route="/pruebas" /> */}
159 148
 
160
-           <ListItem selected={ SubMenuList.includes(selectedIndex) } onClick={showPruebas}>
161
-           {/* <ListItem selected={  SubMenuIndex.includes(selectedIndex) && !open } onClick={showPruebas}> */}
149
+            <ListItem selected={ SubMenuList.includes(selectedIndex) } onClick={showPruebas}>
162 150
                 <ListItemIcon>
163 151
                     <FingerprintIcon />
164 152
                 </ListItemIcon>
165 153
 
166 154
 
167 155
                 <ListItemText 
168
-                sx={{
169
-                    fontSize: 12,
170
-                    ' .css-10hburv-MuiTypography-root' : {
171
-                        fontSize : '.875rem'
172
-                    },
173
-                }}
174
-                primary="Pruebas" 
156
+                    sx={{
157
+                        fontSize: 12,
158
+                        ' .css-10hburv-MuiTypography-root' : {
159
+                            fontSize : '.875rem'
160
+                        },
161
+                    }}
162
+                    primary="Pruebas" 
175 163
                 />
176 164
                 {open ? <ExpandLess /> : <ExpandMore />}
177 165
             </ListItem>
@@ -179,7 +167,7 @@ export const MainListItems = (props) =>  {
179 167
 
180 168
             <Collapse in={open} timeout="auto" unmountOnExit>
181 169
                 <List component="div" disablePadding>
182
-                        
170
+
183 171
                     <SubMenuItem route="pruebas/crear" selected={selectedIndex} index={5} change={handleListItemClick} title="Crear Prueba" />
184 172
                     <SubMenuItem route="pruebas/listar" change={handleListItemClick} selected={selectedIndex} index={6} title="Listado de pruebas" />
185 173
                     <SubMenuItem route="pruebas/aplicar"  selected={selectedIndex} index={7} change={handleListItemClick} title="Aplicar" />

+ 3 - 3
src/Pages/404.jsx

@@ -2,9 +2,9 @@ import { Col, Row }  from 'react-bootstrap'
2 2
 
3 3
 export function NotFound() {
4 4
     return(
5
-        <section class="error_page">
6
-            <div class="page_notfound">
7
-                <Row class="row">
5
+        <section className="error_page">
6
+            <div className="page_notfound">
7
+                <Row className="row">
8 8
                     <Col md="12">
9 9
                         <h1>404</h1>
10 10
                         <h3>Upss!.. Algo salió mal</h3>