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 {