From 081fcab7ebb5eca550a0310d750b341a4dcd7d70 Mon Sep 17 00:00:00 2001 From: wangdan-fit2cloud <79562285+wangdan-fit2cloud@users.noreply.github.com> Date: Tue, 1 Apr 2025 16:19:13 +0800 Subject: [PATCH] fix: Token conflict issue between two different applications embedded in the same system(#2753) --- ui/src/stores/modules/application.ts | 4 ++-- ui/src/stores/modules/user.ts | 14 ++++++++------ ui/src/views/chat/index.vue | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ui/src/stores/modules/application.ts b/ui/src/stores/modules/application.ts index 61a41dd81..193eed479 100644 --- a/ui/src/stores/modules/application.ts +++ b/ui/src/stores/modules/application.ts @@ -97,8 +97,8 @@ const useApplicationStore = defineStore({ applicationApi .postAppAuthentication(token, loading, authentication_value) .then((res) => { - localStorage.setItem('accessToken', res.data) - sessionStorage.setItem('accessToken', res.data) + localStorage.setItem(`${token}accessToken`, res.data) + sessionStorage.setItem(`${token}accessToken`, res.data) resolve(res) }) .catch((error) => { diff --git a/ui/src/stores/modules/user.ts b/ui/src/stores/modules/user.ts index 2b4d20417..1f98d8045 100644 --- a/ui/src/stores/modules/user.ts +++ b/ui/src/stores/modules/user.ts @@ -13,7 +13,7 @@ export interface userStateTypes { userInfo: User | null token: any version?: string - accessToken?: string + userAccessToken?: string XPACK_LICENSE_IS_VALID: false isXPack: false themeInfo: any @@ -26,6 +26,7 @@ const useUserStore = defineStore({ userInfo: null, token: '', version: '', + userAccessToken: '', XPACK_LICENSE_IS_VALID: false, isXPack: false, themeInfo: null @@ -60,11 +61,11 @@ const useUserStore = defineStore({ return this.userType === 1 ? localStorage.getItem('token') : this.getAccessToken() }, getAccessToken() { - const accessToken = sessionStorage.getItem('accessToken') - if (accessToken) { - return accessToken + const token = sessionStorage.getItem(`${this.userAccessToken}accessToken`) + if (token) { + return token } - return localStorage.getItem('accessToken') + return localStorage.getItem(`${this.userAccessToken}accessToken`) }, getPermissions() { @@ -83,8 +84,9 @@ const useUserStore = defineStore({ return '' } }, - changeUserType(num: number) { + changeUserType(num: number, token?: string) { this.userType = num + this.userAccessToken = token }, async asyncGetProfile() { diff --git a/ui/src/views/chat/index.vue b/ui/src/views/chat/index.vue index d2fd0ed60..7f58d1df3 100644 --- a/ui/src/views/chat/index.vue +++ b/ui/src/views/chat/index.vue @@ -92,7 +92,7 @@ function getAccessToken(token: string) { }) } onBeforeMount(() => { - user.changeUserType(2) + user.changeUserType(2, accessToken) Promise.all([user.asyncGetProfile(), getAccessToken(accessToken)]) .catch(() => { applicationAvailable.value = false