Skip to content

Commit ad1d8c1

Browse files
committed
feat(projects): clear tabs cache when switching users.
1 parent c3abc3d commit ad1d8c1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/store/modules/auth/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
4141
async function resetStore() {
4242
const authStore = useAuthStore();
4343

44+
recordUserId();
45+
4446
clearAuthStorage();
4547

4648
authStore.$reset();
@@ -53,6 +55,33 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
5355
routeStore.resetStore();
5456
}
5557

58+
/** Record the user ID of the previous login session Used to compare with the current user ID on next login */
59+
function recordUserId() {
60+
if (!userInfo.userId) {
61+
return;
62+
}
63+
64+
// Store current user ID locally for next login comparison
65+
localStg.set('lastLoginUserId', userInfo.userId);
66+
}
67+
68+
/** Check if current login user is different from previous login user If different, clear all tabs */
69+
function checkTabClear() {
70+
if (!userInfo.userId) {
71+
return;
72+
}
73+
74+
const lastLoginUserId = localStg.get('lastLoginUserId');
75+
76+
// Clear all tabs if current user is different from previous user
77+
if (!lastLoginUserId || lastLoginUserId !== userInfo.userId) {
78+
localStg.remove('globalTabs');
79+
tabStore.clearTabs();
80+
}
81+
82+
localStg.remove('lastLoginUserId');
83+
}
84+
5685
/**
5786
* Login
5887
*
@@ -95,6 +124,9 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
95124
if (pass) {
96125
token.value = loginToken.token;
97126

127+
// Check if the tab needs to be cleared
128+
checkTabClear();
129+
98130
return true;
99131
}
100132

src/typings/storage.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ declare namespace StorageType {
3737
layout: UnionKey.ThemeLayoutMode;
3838
siderCollapse: boolean;
3939
};
40+
/** The last login user id */
41+
lastLoginUserId: string;
4042
}
4143
}

0 commit comments

Comments
 (0)