Browse Source

auth flow elementary fixes

amenpunk 3 years ago
parent
commit
7c8ba5194b
3 changed files with 23 additions and 15 deletions
  1. 13 4
      src/Auth/AuthProvider.js
  2. 3 11
      src/Components/Routes.js
  3. 7 0
      src/Pages/Login.jsx

+ 13 - 4
src/Auth/AuthProvider.js

26
             setUser(undefined)
26
             setUser(undefined)
27
         },
27
         },
28
         isLogged : () => {
28
         isLogged : () => {
29
-            let CookiesUser = Cookies.get('user')
30
-            console.log('existe usuario -> ', CookiesUser)
31
-            if(!CookiesUser || CookiesUser === 'undefined'){
29
+            try{
30
+                let CookiesUser = Cookies.get('user')
31
+                if(!CookiesUser || CookiesUser === 'undefined'){
32
+                    return false
33
+                }
34
+                let current = JSON.parse(CookiesUser);
35
+                // console.log("CURR", current)
36
+                let logged = Object.keys(current).includes('email')
37
+                console.log("is login? >>", logged)
38
+                return logged
39
+
40
+            }catch(e){
41
+                console.log("Error checking user ", e)
32
                 return false
42
                 return false
33
             }
43
             }
34
-            return !!CookiesUser
35
         }
44
         }
36
     }
45
     }
37
 
46
 

+ 3 - 11
src/Components/Routes.js

1
 import React, {useEffect} from 'react'
1
 import React, {useEffect} from 'react'
2
-import { Routes, Route,useNavigate } from "react-router-dom";
2
+import { Routes, Route,useNavigate, Navigate } from "react-router-dom";
3
 
3
 
4
 import { Dashboard } from "./Dashboard";
4
 import { Dashboard } from "./Dashboard";
5
 import { Login } from '../Pages/Login'
5
 import { Login } from '../Pages/Login'
16
 import { NotFound } from '../Pages/404'
16
 import { NotFound } from '../Pages/404'
17
 import { Profile } from '../Pages/Profile'
17
 import { Profile } from '../Pages/Profile'
18
 
18
 
19
-import useAuth from '../Auth/useAuth'
20
 import RequireAuth from '../Components/PrivateRoute'
19
 import RequireAuth from '../Components/PrivateRoute'
21
 
20
 
22
 export default function MyRoutes () {
21
 export default function MyRoutes () {
23
 
22
 
24
-    let auth = useAuth();
25
-    let navigate = useNavigate()
26
-
27
-    useEffect(() => {
28
-        if(!auth.isLogged()){
29
-            return navigate('login')
30
-        }    
31
-    }, [auth, navigate])
32
-
33
     return(
23
     return(
34
         <Routes>
24
         <Routes>
35
 
25
 
26
+            <Route path="/" element={<Navigate to='login'/>} />
27
+            <Route path="psicoadmin" element={<Navigate to='/login'/>} />
36
             <Route path="login" element={<Login/>} />
28
             <Route path="login" element={<Login/>} />
37
             <Route 
29
             <Route 
38
                 path="dashboard" 
30
                 path="dashboard" 

+ 7 - 0
src/Pages/Login.jsx

46
         return navigate('/dashboard/home')
46
         return navigate('/dashboard/home')
47
     };
47
     };
48
 
48
 
49
+    React.useEffect(() => {
50
+        if(auth.isLogged()){
51
+            return navigate('/dashboard/home')
52
+        }    
53
+    }, [])
54
+
55
+
49
     return (
56
     return (
50
         <ThemeProvider theme={theme}>
57
         <ThemeProvider theme={theme}>
51
             <Grid container component="main" sx={{ height: '100vh' }}>
58
             <Grid container component="main" sx={{ height: '100vh' }}>