From 43e2287fd943285f25cd87a1802c5f222ab0102a Mon Sep 17 00:00:00 2001 From: awinx Date: Mon, 16 Dec 2024 12:16:46 +0800 Subject: [PATCH] update --- src/router/index.js | 36 ++++++++++-- src/views/course/CourseList.vue | 4 +- src/views/login/loginView.vue | 6 +- src/views/student/studentList.vue | 98 ++++++++++++++++++++++++++++++- 4 files changed, 133 insertions(+), 11 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index f4ff8ab..12f7dd3 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -5,11 +5,11 @@ import VueRouter from 'vue-router' Vue.use(VueRouter) const routes = [ - { - path: '/', - name: 'home', - component: () => import('../App.vue') - }, + // { + // path: '/', + // name: 'home', + // component: () => import('../App.vue') + // }, { path: '/course', name: 'course', @@ -20,6 +20,11 @@ const routes = [ path: '/login', name: 'login', component: () => import('../views/login/loginView.vue'), + }, + { + path: '/student', + name: 'student', + component: () => import('../views/student/studentList.vue'), } ] @@ -27,9 +32,28 @@ const router = new VueRouter({ routes }) +function JwtExpireCheck(jwt) { + if(!jwt){ + return false; + } + const parts = jwt.split('.'); + if (parts.length !== 3) { + return false; + } + const payload = JSON.parse(atob(parts[1])); + if (payload.exp) { + return (new Date(payload.exp)>new Date()/1000) ; + } + throw new Error('No expiration time found in JWT payload'); +} +import $ from 'jquery' router.beforeEach((to, _, next) => { const isLoggedIn = localStorage.getItem('user'); - if (!to.meta.requiresAuth || isLoggedIn) { + console.log(localStorage.getItem('user')); + if (!to.meta.requiresAuth || JwtExpireCheck(isLoggedIn)) { + $.ajaxSetup({ + headers: { 'token': isLoggedIn } + }); next(); } else { diff --git a/src/views/course/CourseList.vue b/src/views/course/CourseList.vue index a3d1794..fa3dc98 100644 --- a/src/views/course/CourseList.vue +++ b/src/views/course/CourseList.vue @@ -1,7 +1,7 @@