From 09d312d07602d51c6ad30eec5768d45d28e14c0f Mon Sep 17 00:00:00 2001 From: jingrow Date: Fri, 21 Nov 2025 09:22:50 +0800 Subject: [PATCH] fix: auto logout when cookie expires (user_id becomes Guest) --- apps/jingrow/frontend/src/shared/stores/auth.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/apps/jingrow/frontend/src/shared/stores/auth.ts b/apps/jingrow/frontend/src/shared/stores/auth.ts index c14703a..a024574 100644 --- a/apps/jingrow/frontend/src/shared/stores/auth.ts +++ b/apps/jingrow/frontend/src/shared/stores/auth.ts @@ -1,6 +1,6 @@ import { defineStore } from 'pinia' import { ref, computed } from 'vue' -import { loginApi, getUserInfoApi, logoutApi, getSessionUser, isCookieExpired } from '../api/auth' +import { loginApi, getUserInfoApi, logoutApi, isCookieExpired } from '../api/auth' export interface User { id: string @@ -59,6 +59,17 @@ export const useAuthStore = defineStore('auth', () => { } const initAuth = async () => { + // 检查user_id是否为Guest(cookie过期时后端会设置为Guest) + const cookies = new URLSearchParams(document.cookie.split('; ').join('&')) + const userId = cookies.get('user_id') + if (userId === 'Guest') { + // user_id是Guest,说明cookie已过期,清除本地状态 + if (isAuthenticated.value) { + await logout() + } + return + } + // 首先检查session cookie是否存在 if (!isCookieExpired()) { try {