浏览代码

remove token in user profile

amenpunk 2 年之前
父节点
当前提交
53252377de
共有 2 个文件被更改,包括 30 次插入5 次删除
  1. 6 2
      src/Components/Dashboard.js
  2. 24 3
      src/Components/User.jsx

+ 6 - 2
src/Components/Dashboard.js

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

+ 24 - 3
src/Components/User.jsx

@@ -27,6 +27,7 @@ import Footer from "../Components/Footer";
27 27
 
28 28
 import { Drawer as MuiDrawer, AppBar as MuiAppBar } from "../Components/Navigation/AppBar"
29 29
 import { UserListItems, SecondaryListItems } from '../Components/Navigation/listItems';
30
+import jwtDecode from 'jwt-decode';
30 31
 // import ProfilePicture from '../Images/man.png';
31 32
 
32 33
 const drawerWidth = 240;
@@ -86,8 +87,8 @@ export function User() {
86 87
   const navigate = useNavigate()
87 88
   let dispatch = useDispatch()
88 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 92
   const CerrarSession = () => {
92 93
     dispatch(removeToken({ candi: true }))
93 94
     navigate('/logincd')
@@ -105,7 +106,27 @@ export function User() {
105 106
     }
106 107
   }
107 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 131
   return (
111 132
     <ThemeProvider theme={mdTheme}>