Browse Source

remove token in user profile

amenpunk 2 years ago
parent
commit
53252377de
2 changed files with 30 additions and 5 deletions
  1. 6 2
      src/Components/Dashboard.js
  2. 24 3
      src/Components/User.jsx

+ 6 - 2
src/Components/Dashboard.js

88
   const auth = useSelector((state) => state.token.token)
88
   const auth = useSelector((state) => state.token.token)
89
 
89
 
90
   const CerrarSession = () => {
90
   const CerrarSession = () => {
91
-    //TODO:
92
-    //remove all status
93
     dispatch(removeToken())
91
     dispatch(removeToken())
94
     navigate('/')
92
     navigate('/')
95
   }
93
   }
121
       alert("TOKEN EXPIRADO, PORFAVOR INICIA SESSION")
119
       alert("TOKEN EXPIRADO, PORFAVOR INICIA SESSION")
122
       dispatch(removeToken())
120
       dispatch(removeToken())
123
     }
121
     }
122
+
123
+    setTimeout(() => {
124
+      alert("TOKEN EXPIRADO, PORFAVOR INICIA SESSION")
125
+
126
+    }, exp_in)
127
+
124
   }, [isMovil,auth, dispatch])
128
   }, [isMovil,auth, dispatch])
125
 
129
 
126
   return (
130
   return (

+ 24 - 3
src/Components/User.jsx

27
 
27
 
28
 import { Drawer as MuiDrawer, AppBar as MuiAppBar } from "../Components/Navigation/AppBar"
28
 import { Drawer as MuiDrawer, AppBar as MuiAppBar } from "../Components/Navigation/AppBar"
29
 import { UserListItems, SecondaryListItems } from '../Components/Navigation/listItems';
29
 import { UserListItems, SecondaryListItems } from '../Components/Navigation/listItems';
30
+import jwtDecode from 'jwt-decode';
30
 // import ProfilePicture from '../Images/man.png';
31
 // import ProfilePicture from '../Images/man.png';
31
 
32
 
32
 const drawerWidth = 240;
33
 const drawerWidth = 240;
86
   const navigate = useNavigate()
87
   const navigate = useNavigate()
87
   let dispatch = useDispatch()
88
   let dispatch = useDispatch()
88
   let profile = useSelector((state) => state.user.profile)
89
   let profile = useSelector((state) => state.user.profile)
89
-  let name = `${(profile?.email.substring(0, 1))} ${profile?.email.substring(1, 2)}`
90
-
90
+  const auth = useSelector((state) => state.token.candi)
91
+  let name = profile ? `${(profile?.email.substring(0, 1))} ${profile?.email.substring(1, 2)}` : ""
91
   const CerrarSession = () => {
92
   const CerrarSession = () => {
92
     dispatch(removeToken({ candi: true }))
93
     dispatch(removeToken({ candi: true }))
93
     navigate('/logincd')
94
     navigate('/logincd')
105
     }
106
     }
106
   }
107
   }
107
   const [anchorElMovil, setAnchorElMov] = React.useState(false);
108
   const [anchorElMovil, setAnchorElMov] = React.useState(false);
108
-  React.useEffect(() => isMovil ? setOpen(false) : null, [isMovil])
109
+  React.useEffect(() => { 
110
+
111
+    const { exp } = jwtDecode(auth.token)
112
+    let timestamp = exp * 1000;
113
+    let exp_in = timestamp - Date.now();
114
+    console.log("EXP IN : ", exp_in)
115
+    if(exp_in <= 0 ){
116
+      alert("TOKEN EXPIRADO, PORFAVOR INICIA SESSION")
117
+      dispatch(removeToken({ candi: true}))
118
+    }
119
+
120
+    setTimeout(() => {
121
+      alert("TOKEN EXPIRADO, PORFAVOR INICIA SESSION")
122
+
123
+    }, exp_in)
124
+
125
+
126
+    if(isMovil){
127
+      setOpen(false)
128
+    }
129
+  }, [isMovil, auth, dispatch])
109
 
130
 
110
   return (
131
   return (
111
     <ThemeProvider theme={mdTheme}>
132
     <ThemeProvider theme={mdTheme}>