From 2d7be8f7c47ebc8980a0738525b696c77491bea0 Mon Sep 17 00:00:00 2001 From: jingrow Date: Sat, 3 Jan 2026 01:09:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4localStorage?= =?UTF-8?q?=E5=AD=98=E5=82=A8=EF=BC=8C=E6=94=B9=E4=B8=BA=E5=9F=BA=E4=BA=8E?= =?UTF-8?q?cookie=E7=9A=84=E7=8A=B6=E6=80=81=E4=BF=9D=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除所有localStorage相关代码,不再保存敏感信息 - 前端UserInfo接口只保留后端实际返回的字段(user, user_type) - 更新getUserInfoApi和loginApi,只映射后端实际返回的字段 - 更新UserMenu组件,使用user字段替代username - 状态保持完全基于cookie验证 --- src/shared/api/auth.ts | 43 +++++++++------------------- src/shared/components/UserMenu.vue | 5 ++-- src/shared/stores/auth.ts | 46 +++++------------------------- 3 files changed, 23 insertions(+), 71 deletions(-) diff --git a/src/shared/api/auth.ts b/src/shared/api/auth.ts index 6048f34..a24ffc2 100644 --- a/src/shared/api/auth.ts +++ b/src/shared/api/auth.ts @@ -4,12 +4,7 @@ export interface LoginResponse { } export interface UserInfo { - id: string - username: string - email: string - avatar: string - first_name: string - last_name: string + user: string user_type: string } @@ -63,26 +58,21 @@ export const loginApi = async (username: string, password: string): Promise setTimeout(resolve, 200)) - const detailedUserInfo = await getUserInfoApi() - if (detailedUserInfo.id && detailedUserInfo.id !== 'Guest') { - return { message: data.message, user: detailedUserInfo } + const userInfo = await getUserInfoApi() + if (userInfo.user && userInfo.user !== 'Guest') { + return { message: data.message, user: userInfo } } } catch { - // API调用失败不影响登录 + // API调用失败不影响登录,使用默认值 + } + + // 如果获取用户信息失败,使用默认值 + const userInfo: UserInfo = { + user: username, + user_type: data.message === 'No App' ? 'Website User' : 'System User' } return { message: data.message, user: userInfo } @@ -117,16 +107,11 @@ export const getUserInfoApi = async (): Promise => { const userInfo = data.message || data const formattedUserInfo: UserInfo = { - id: userInfo.user || userInfo.name || userInfo.username || '', - username: userInfo.user || userInfo.name || userInfo.username || '', - email: userInfo.email || '', - avatar: userInfo.user_image || '', - first_name: userInfo.first_name || '', - last_name: userInfo.last_name || '', + user: userInfo.user || '', user_type: userInfo.user_type || 'System User' } - if (!formattedUserInfo.id || formattedUserInfo.id === 'Guest') { + if (!formattedUserInfo.user || formattedUserInfo.user === 'Guest') { throw new Error('无法解析用户信息') } diff --git a/src/shared/components/UserMenu.vue b/src/shared/components/UserMenu.vue index 58e306b..713fec0 100644 --- a/src/shared/components/UserMenu.vue +++ b/src/shared/components/UserMenu.vue @@ -8,11 +8,10 @@ - {{ user?.username?.charAt(0).toUpperCase() }} + {{ user?.user?.charAt(0).toUpperCase() }} - {{ user?.username }} + {{ user?.user }} diff --git a/src/shared/stores/auth.ts b/src/shared/stores/auth.ts index 96cbcd5..2fabfe1 100644 --- a/src/shared/stores/auth.ts +++ b/src/shared/stores/auth.ts @@ -4,13 +4,8 @@ import { loginApi, getUserInfoApi, logoutApi, isCookieExpired, getSessionUser } import { setInitializingAuth } from '../utils/fetchInterceptor' export interface User { - id: string - username: string - email: string - avatar?: string - first_name?: string - last_name?: string - user_type?: string + user: string + user_type: string } export const useAuthStore = defineStore('auth', () => { @@ -28,39 +23,16 @@ export const useAuthStore = defineStore('auth', () => { error?.message?.includes('Cookie已过期') } - // 设置用户状态(统一的状态更新方法) + // 设置用户状态(统一的状态更新方法,不保存到localStorage) const setUserState = (userInfo: User) => { user.value = userInfo isAuthenticated.value = true - localStorage.setItem('jingrow_user', JSON.stringify(userInfo)) - localStorage.setItem('jingrow_authenticated', 'true') } // 清除用户状态 const clearUserState = () => { user.value = null isAuthenticated.value = false - localStorage.removeItem('jingrow_user') - localStorage.removeItem('jingrow_authenticated') - } - - // 从localStorage恢复用户状态 - const restoreUserFromStorage = (): User | null => { - const savedUser = localStorage.getItem('jingrow_user') - const savedAuth = localStorage.getItem('jingrow_authenticated') - - if (savedUser && savedAuth === 'true') { - try { - const parsedUser = JSON.parse(savedUser) - user.value = parsedUser - isAuthenticated.value = true - return parsedUser - } catch (error) { - console.error('解析保存的用户信息失败:', error) - clearUserState() - } - } - return null } // 验证并更新用户信息 @@ -116,20 +88,16 @@ export const useAuthStore = defineStore('auth', () => { const hasSessionCookie = !isCookieExpired() const hasCookie = userId || hasSessionCookie - // 尝试从localStorage恢复状态(避免闪烁) - const savedUser = restoreUserFromStorage() - const hasSavedState = !!savedUser - - // 如果既没有cookie也没有保存的状态,清除认证 - if (!hasCookie && !hasSavedState) { + // 如果没有cookie,清除认证状态 + if (!hasCookie) { if (isAuthenticated.value) { clearUserState() } return } - // 如果有cookie或保存的状态,尝试验证 - if (hasCookie || hasSavedState) { + // 如果有cookie,尝试验证并获取用户信息 + if (hasCookie) { await validateAndUpdateUser() } } finally {