From e25f252bede632f964ca1224d151ea21e465a9d0 Mon Sep 17 00:00:00 2001 From: liangweihao <734499798@qq.com> Date: Mon, 21 Jul 2025 21:43:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/index.ts | 3 + frontend/src/stores/admin.ts | 9 +- frontend/src/stores/auth.ts | 13 ++- frontend/src/views/Test.vue | 212 ----------------------------------- 4 files changed, 20 insertions(+), 217 deletions(-) delete mode 100644 frontend/src/views/Test.vue diff --git a/backend/src/index.ts b/backend/src/index.ts index b6f9061..b83f8e2 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -47,6 +47,9 @@ app.use(express.urlencoded({ extended: true })); // Request logging app.use((req, res, next) => { + if (req.path === '/health') { + return next(); + } logger.info(`${req.method} ${req.path}`, { ip: req.ip, userAgent: req.get('User-Agent') diff --git a/frontend/src/stores/admin.ts b/frontend/src/stores/admin.ts index 8487227..af89500 100644 --- a/frontend/src/stores/admin.ts +++ b/frontend/src/stores/admin.ts @@ -22,13 +22,20 @@ export const useAdminStore = defineStore('admin', () => { const isLoggedIn = computed(() => !!token.value && !!admin.value) // 初始化认证状态 - const initAuth = () => { + const initAuth = async () => { const storedToken = adminAuth.getToken() const storedAdmin = adminAuth.getAdminInfo() if (storedToken && storedAdmin) { token.value = storedToken admin.value = storedAdmin + try { + await getStats() + } catch (error) { + // token失效,清除本地存储 + logout() + console.error('Admin token is invalid, logging out') + } } } diff --git a/frontend/src/stores/auth.ts b/frontend/src/stores/auth.ts index 45cf696..67bdb91 100644 --- a/frontend/src/stores/auth.ts +++ b/frontend/src/stores/auth.ts @@ -15,15 +15,20 @@ export const useAuthStore = defineStore('auth', () => { const isLoggedIn = computed(() => !!token.value && !!user.value) // 初始化认证状态 - const initAuth = () => { + const initAuth = async () => { const storedToken = userAuth.getToken() const storedUser = userAuth.getUserInfo() - + if (storedToken && storedUser) { token.value = storedToken user.value = storedUser - } else { - + try { + await getProfile() + } catch (error) { + // token失效,清除本地存储 + logout() + console.error('Token is invalid, logging out') + } } } diff --git a/frontend/src/views/Test.vue b/frontend/src/views/Test.vue deleted file mode 100644 index 8c16496..0000000 --- a/frontend/src/views/Test.vue +++ /dev/null @@ -1,212 +0,0 @@ - - - - - API 集成测试 - - - - - - - 认证状态 - - - - - - 登录状态 - - {{ authStore.isLoggedIn ? '已登录' : '未登录' }} - - - - 用户信息 - - {{ authStore.user?.username || '无' }} - - - - - - - - - - - API 测试 - - - - - - - {{ loading ? '测试中...' : '测试获取路径' }} - - - 结果: {{ pathsResult }} - - - - - - - {{ loading ? '测试中...' : '测试用户注册' }} - - - 结果: {{ registerResult }} - - - - - - - {{ loading ? '测试中...' : '测试用户登录' }} - - - 结果: {{ loginResult }} - - - - - - - {{ loading ? '测试中...' : '测试获取用户信息' }} - - - 结果: {{ profileResult }} - - - - - - - - - - 响应数据 - - - - {{ responseData }} - - - - - - - \ No newline at end of file
登录状态
- {{ authStore.isLoggedIn ? '已登录' : '未登录' }} -
用户信息
- {{ authStore.user?.username || '无' }} -
{{ responseData }}