From 4b3ebaa7ed53eab85ca035e371c8183376e0a22a Mon Sep 17 00:00:00 2001 From: jingrow Date: Sun, 4 Jan 2026 19:16:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E4=BF=A1=E6=81=AF=E5=9C=A8localStorage=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E4=BF=9D=E5=AD=98=EF=BC=8C=E6=94=B9=E4=B8=BA=E4=BB=85?= =?UTF-8?q?=E4=BD=BF=E7=94=A8cookies=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 auth store 中所有 localStorage 相关代码(loadUserFromStorage、saveUserToStorage、clearUserFromStorage) - 简化 setUserState 和 clearUserState,不再操作 localStorage - 修改 initAuth 逻辑,只依赖 cookies 验证登录状态,不再从 localStorage 恢复 - 移除 HomePage.vue 和 Signup.vue 中注册成功后的 localStorage 保存 - 登录状态完全由后端 cookies 控制,提高安全性 --- src/shared/stores/auth.ts | 54 +++------------------------------------ src/views/HomePage.vue | 2 -- src/views/auth/Signup.vue | 2 -- 3 files changed, 3 insertions(+), 55 deletions(-) diff --git a/src/shared/stores/auth.ts b/src/shared/stores/auth.ts index 063c7e4..ea853e8 100644 --- a/src/shared/stores/auth.ts +++ b/src/shared/stores/auth.ts @@ -8,8 +8,6 @@ export interface User { user_type: string } -const STORAGE_KEY = 'auth_user' - export const useAuthStore = defineStore('auth', () => { const user = ref(null) const loading = ref(false) @@ -25,49 +23,16 @@ export const useAuthStore = defineStore('auth', () => { error?.message?.includes('Cookie已过期') } - // 从 localStorage 加载用户信息 - const loadUserFromStorage = (): User | null => { - try { - const stored = localStorage.getItem(STORAGE_KEY) - if (stored) { - return JSON.parse(stored) - } - } catch (error) { - console.error('从 localStorage 加载用户信息失败:', error) - } - return null - } - - // 保存用户信息到 localStorage - const saveUserToStorage = (userInfo: User) => { - try { - localStorage.setItem(STORAGE_KEY, JSON.stringify(userInfo)) - } catch (error) { - console.error('保存用户信息到 localStorage 失败:', error) - } - } - - // 清除 localStorage 中的用户信息 - const clearUserFromStorage = () => { - try { - localStorage.removeItem(STORAGE_KEY) - } catch (error) { - console.error('清除 localStorage 用户信息失败:', error) - } - } - - // 设置用户状态(统一的状态更新方法,保存到localStorage) + // 设置用户状态(只更新内存状态,不保存到 localStorage) const setUserState = (userInfo: User) => { user.value = userInfo isAuthenticated.value = true - saveUserToStorage(userInfo) } // 清除用户状态 const clearUserState = () => { user.value = null isAuthenticated.value = false - clearUserFromStorage() } // 验证并更新用户信息 @@ -131,21 +96,8 @@ export const useAuthStore = defineStore('auth', () => { } } - // 如果cookie验证失败或没有cookie,尝试从localStorage恢复 - const storedUser = loadUserFromStorage() - if (storedUser) { - user.value = storedUser - isAuthenticated.value = true - // 尝试在后台验证用户信息,失败也不影响当前状态 - validateAndUpdateUser().catch(() => { - // 静默失败,保持localStorage中的状态 - }) - } else { - // 既没有cookie也没有localStorage,清除认证状态 - if (isAuthenticated.value) { - clearUserState() - } - } + // 如果没有cookie或cookie验证失败,清除认证状态 + clearUserState() } finally { setInitializingAuth(false) } diff --git a/src/views/HomePage.vue b/src/views/HomePage.vue index e84671c..2ca419e 100644 --- a/src/views/HomePage.vue +++ b/src/views/HomePage.vue @@ -182,8 +182,6 @@ const handleSignupSubmit = async () => { if (result.user) { authStore.user = result.user authStore.isAuthenticated = true - localStorage.setItem('jingrow_user', JSON.stringify(result.user)) - localStorage.setItem('jingrow_authenticated', 'true') showSignupModal.value = false signupFormData.username = '' signupFormData.password = '' diff --git a/src/views/auth/Signup.vue b/src/views/auth/Signup.vue index 25f89ce..4954c7d 100644 --- a/src/views/auth/Signup.vue +++ b/src/views/auth/Signup.vue @@ -230,8 +230,6 @@ const handleSignup = async () => { if (result.user) { authStore.user = result.user authStore.isAuthenticated = true - localStorage.setItem('jingrow_user', JSON.stringify(result.user)) - localStorage.setItem('jingrow_authenticated', 'true') router.push('/') } else { const loginResult = await authStore.login(formData.username, formData.password)