-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: Token conflict issue between two different applications embedded… #2766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two main issues in the updated code:
To optimize and fix these errors, here's a revised version: export interface userStateTypes {
userInfo: User | null;
token: any;
version?: string;
userAccessToken: string; // Removed "?" because we'll always have an access token.
XPACK_LICENSE_IS_VALID: false;
isXPack: false;
themeInfo: any;
}
const useUserStore = defineStore({
state: () => ({
userInfo: null,
token: '',
version: '',
userAccessToken: localStorage.getItem('token') || '', // Default to empty string if not found.
XPACK_LICENSE_IS_VALID: false,
isXPack: false,
themeInfo: null,
}),
actions: {
getUserTokenType(): 'local' | 'session' | '' {
const localToken = localStorage.getItem(this.userAccessToken);
const sessionToken = sessionStorage.getItem(`${this.userAccessToken}accessToken`);
return localToken != null ? 'local' : (sessionToken != null ? 'session' : '');
},
getAccessToken(): string {
return localStorage.getItem(this.userAccessToken) ||
sessionStorage.getItem(`${this.userAccessToken}accessToken`);
},
getPermissions() {
if (!this.userInfo || !this.token) {
return '';
}
// Retrieve permissions from UserInfo based on this.token
// ...
},
changeUserType(num: number, token: string) {
this.userType = num;
this.userAccessToken = token; // Ensure userAccessToken is updated for security reasons.
// Invalidate tokens if needed.
localStorage.removeItem(this.userAccessToken);
sessionStorage.removeItem(`${this.userAccessToken}access_token`);
// Optionally, save new token locally with proper timestamp or encryption.
localStorage.setItem(this.userAccessToken, token);
// Save session token as well.
sessionStorage.setItem(`${this.userAccessToken}access_token`, token);
},
async asyncGetProfile() {
try {
let urlParams = new URLSearchParams(new URL(window.location.href).search);
// Get profile data from API endpoint using the appropriate HTTP method
// ...
} catch(e) {
console.error("An error occurred while fetching your profile:", e);
throw new Error("Failed to fetch profile");
}
}
}
});Key Changes:
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code has a few issues that need to be addressed:
Here's an optimized version of the code, assuming async function fetchUserData() {
try {
await user.changeUserType(2);
const [profileData, accessToken] = await Promise.all([
user.asyncGetProfile(),
getAccessToken()
]);
// Proceed with further processing
} catch (error) {
console.error("Error fetching data:", error);
applicationAvailable.value = false;
}
}
onBeforeMount(fetchUserData);This refactoring separates the logic inside |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The revised code adds the token to each storage key, which is beneficial if you have multiple tokens (e.g., developer access vs. user access). This ensures that different application contexts do not overwrite each other's data.
This change will ensure that each token has its own set of stored access tokens, enhancing security and clarity when managing concurrent sessions across different users or systems interacting with your API endpoints.