fix: auto logout when cookie expires (user_id becomes Guest)

This commit is contained in:
jingrow 2025-11-21 09:22:50 +08:00
parent 67a117f65f
commit 09d312d076

View File

@ -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是否为Guestcookie过期时后端会设置为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 {