Skip to content

Commit 4b661e3

Browse files
committed
Implement automatic logout for WebUI
1 parent a0de70f commit 4b661e3

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

webapp/src/api/base.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export class ClientApi {
1414
headers: getHeaders(),
1515
method: "GET"
1616
});
17-
if (res.status !== 200) {
17+
if (res.status === 401 || res.status === 403) {
18+
this.authErrorHandler()
19+
}
20+
else if (res.status !== 200) {
1821
Toast.error({
1922
content: Locale.Error.Network,
2023
duration: 5,
@@ -23,15 +26,8 @@ export class ClientApi {
2326
throw new Error(Locale.Error.Network);
2427
}
2528
const resJson = await res.json();
26-
if (resJson.code === 401) {
27-
Toast.error({
28-
content: Locale.Auth.Expiration,
29-
duration: 5,
30-
theme: "light"
31-
});
32-
const accessStore = useAccessStore.getState();
33-
accessStore.updateToken("");
34-
throw new Error(Locale.Auth.Expiration);
29+
if (resJson.code === 401 || resJson.code === 403) {
30+
this.authErrorHandler()
3531
} else if (resJson.code !== 200) {
3632
Toast.error({
3733
content: resJson.msg,
@@ -54,7 +50,10 @@ export class ClientApi {
5450
},
5551
method: "POST"
5652
});
57-
if (res.status !== 200) {
53+
if (res.status === 401 || res.status === 403) {
54+
this.authErrorHandler()
55+
}
56+
else if (res.status !== 200) {
5857
Toast.error({
5958
content: Locale.Error.Network,
6059
duration: 5,
@@ -63,15 +62,8 @@ export class ClientApi {
6362
throw new Error(Locale.Error.Network);
6463
}
6564
const resJson = await res.json();
66-
if (resJson.code === 401) {
67-
Toast.error({
68-
content: Locale.Auth.Expiration,
69-
duration: 5,
70-
theme: "light"
71-
});
72-
const accessStore = useAccessStore.getState();
73-
accessStore.updateToken("");
74-
throw new Error(Locale.Auth.Expiration);
65+
if (resJson.code === 401 || resJson.code === 403) {
66+
this.authErrorHandler()
7567
} else if (resJson.code !== 200) {
7668
Toast.error({
7769
content: resJson.msg,
@@ -116,6 +108,17 @@ export class ClientApi {
116108
const proxyPath = import.meta.env.VITE_PROXY_PATH;
117109
return [proxyPath, path].join("");
118110
}
111+
112+
authErrorHandler(): void {
113+
Toast.error({
114+
content: Locale.Auth.Expiration,
115+
duration: 5,
116+
theme: "light"
117+
});
118+
const accessStore = useAccessStore.getState();
119+
accessStore.updateToken("");
120+
throw new Error(Locale.Auth.Expiration);
121+
}
119122
}
120123

121124
export const api = new ClientApi();

0 commit comments

Comments
 (0)