Skip to content

Commit b325fb6

Browse files
committed
fix: avoid license required before login
1 parent af175f4 commit b325fb6

4 files changed

Lines changed: 89 additions & 15 deletions

File tree

frontend/src/api/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ class RequestHttp {
9494
}
9595
if (data.code == ResultEnum.ERRXPACKEE) {
9696
globalStore.isXpackEELicensed = false;
97-
router.push({ name: 'XpackEELicenseRequired' });
97+
const routeName = router.currentRoute.value.name;
98+
if (globalStore.isLogin && routeName !== 'entrance' && routeName !== 'login') {
99+
router.push({ name: 'XpackEELicenseRequired' });
100+
}
98101
return Promise.reject(data);
99102
}
100103
if (data.code == ResultEnum.NodeUnBind) {

frontend/src/layout/components/Sidebar/components/Collapse.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ const logout = () => {
271271
})
272272
.then(async () => {
273273
await logOutApi();
274-
router.push({ name: 'entrance', params: { code: globalStore.entrance } });
275274
globalStore.setLogStatus(false);
276275
globalStore.clearAuthInfo();
276+
router.push({ name: 'entrance', params: { code: globalStore.entrance } });
277277
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
278278
})
279279
.catch(() => {});

frontend/src/layout/components/Sidebar/components/user-info/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,9 +855,9 @@ const onSubmit = async (formEl: FormInstance | undefined) => {
855855
loading.value = false;
856856
open.value = false;
857857
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
858-
router.push({ name: 'entrance', params: { code: globalStore.entrance } });
859858
globalStore.setLogStatus(false);
860859
globalStore.clearAuthInfo();
860+
router.push({ name: 'entrance', params: { code: globalStore.entrance } });
861861
})
862862
.catch(() => {
863863
loading.value = false;

frontend/src/routers/index.ts

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,63 @@ import { MsgError } from '@/utils/message';
99
const axiosCanceler = new AxiosCanceler();
1010

1111
let isRedirecting = false;
12+
let loginStatusLoading: Promise<boolean> | null = null;
1213
let xpackEELoading: Promise<boolean> | null = null;
1314
let licenseStatusLoading: Promise<boolean> | null = null;
15+
let loginStatusLoaded = false;
16+
let xpackEEStatusLoaded = false;
17+
let licenseStatusLoaded = false;
18+
const xpackEELicenseCheckWhiteList = ['XpackEELicenseRequired', 'entrance', 'login', 'Expired'];
19+
20+
const clearLoginStatus = () => {
21+
const globalStore = GlobalStore();
22+
globalStore.setLogStatus(false);
23+
globalStore.clearAuthInfo();
24+
globalStore.isXpackEELicensed = false;
25+
loginStatusLoaded = false;
26+
licenseStatusLoaded = false;
27+
};
28+
29+
const loadLoginStatus = async () => {
30+
const globalStore = GlobalStore();
31+
if (!globalStore.isLogin) {
32+
return false;
33+
}
34+
if (loginStatusLoaded) {
35+
return true;
36+
}
37+
if (!loginStatusLoading) {
38+
loginStatusLoading = fetch('/api/v2/core/auth/current', {
39+
credentials: 'include',
40+
headers: {
41+
CurrentNode: encodeURIComponent(globalStore.currentNode),
42+
},
43+
})
44+
.then((res) => res.json())
45+
.then((res) => {
46+
const loggedIn = res?.code === 200;
47+
if (!loggedIn) {
48+
clearLoginStatus();
49+
return false;
50+
}
51+
loginStatusLoaded = true;
52+
return true;
53+
})
54+
.catch(() => {
55+
clearLoginStatus();
56+
return false;
57+
})
58+
.finally(() => {
59+
loginStatusLoading = null;
60+
});
61+
}
62+
return loginStatusLoading;
63+
};
1464

1565
const loadXpackEEStatus = async () => {
66+
if (xpackEEStatusLoaded) {
67+
return GlobalStore().isXpackEE;
68+
}
1669
if (!xpackEELoading) {
1770
xpackEELoading = fetch('/api/v2/core/auth/setting', {
1871
credentials: 'include',
@@ -21,6 +74,7 @@ const loadXpackEEStatus = async () => {
2174
.then((res) => {
2275
const globalStore = GlobalStore();
2376
globalStore.isXpackEE = !!res?.data?.isXpackEE;
77+
xpackEEStatusLoaded = true;
2478
return globalStore.isXpackEE;
2579
})
2680
.catch(() => GlobalStore().isXpackEE)
@@ -32,6 +86,9 @@ const loadXpackEEStatus = async () => {
3286
};
3387

3488
const loadXpackEELicenseStatus = async () => {
89+
if (licenseStatusLoaded) {
90+
return GlobalStore().isXpackEELicensed;
91+
}
3592
if (!licenseStatusLoading) {
3693
licenseStatusLoading = fetch('/api/v2/core/xpackee/licenses/status', {
3794
credentials: 'include',
@@ -40,7 +97,13 @@ const loadXpackEELicenseStatus = async () => {
4097
},
4198
})
4299
.then((res) => res.json())
43-
.then((res) => res?.data?.status === 'Bound')
100+
.then((res) => {
101+
const globalStore = GlobalStore();
102+
const licensed = res?.data?.status === 'Bound';
103+
globalStore.isXpackEELicensed = licensed;
104+
licenseStatusLoaded = true;
105+
return licensed;
106+
})
44107
.catch(() => false)
45108
.finally(() => {
46109
licenseStatusLoading = null;
@@ -53,7 +116,21 @@ router.beforeEach(async (to, from, next) => {
53116
NProgress.start();
54117
axiosCanceler.removeAllPending();
55118
const globalStore = GlobalStore();
56-
if (to.name !== 'entrance' && to.name !== 'XpackEELicenseRequired' && !globalStore.isLogin) {
119+
if (globalStore.isLogin) {
120+
const loggedIn = await loadLoginStatus();
121+
if (!loggedIn) {
122+
if (to.name !== 'entrance') {
123+
next({
124+
name: 'entrance',
125+
params: to.params,
126+
});
127+
NProgress.done();
128+
return;
129+
}
130+
return next();
131+
}
132+
}
133+
if (to.name !== 'entrance' && !globalStore.isLogin) {
57134
next({
58135
name: 'entrance',
59136
params: to.params,
@@ -76,15 +153,8 @@ router.beforeEach(async (to, from, next) => {
76153
if (globalStore.isLogin && to.name !== 'login') {
77154
await loadXpackEEStatus();
78155
}
79-
if (
80-
globalStore.isLogin &&
81-
globalStore.isXpackEE &&
82-
to.name !== 'XpackEELicenseRequired' &&
83-
to.name !== 'entrance' &&
84-
to.name !== 'login'
85-
) {
86-
const licensed = globalStore.isXpackEELicensed || (await loadXpackEELicenseStatus());
87-
globalStore.isXpackEELicensed = licensed;
156+
if (globalStore.isLogin && globalStore.isXpackEE && !xpackEELicenseCheckWhiteList.includes(String(to.name))) {
157+
const licensed = await loadXpackEELicenseStatus();
88158
if (!licensed) {
89159
next({ name: 'XpackEELicenseRequired', query: { code: String(to.params.code || '') } });
90160
NProgress.done();
@@ -100,7 +170,8 @@ router.beforeEach(async (to, from, next) => {
100170
NProgress.done();
101171
return;
102172
}
103-
if (globalStore.isXpackEELicensed) {
173+
const licensed = await loadXpackEELicenseStatus();
174+
if (licensed) {
104175
next({ name: 'home' });
105176
NProgress.done();
106177
return;

0 commit comments

Comments
 (0)