diff --git a/webapp/src/api/base.ts b/webapp/src/api/base.ts index 37d955f5e..a2b8d9555 100644 --- a/webapp/src/api/base.ts +++ b/webapp/src/api/base.ts @@ -14,6 +14,9 @@ export class ClientApi { headers: getHeaders(), method: "GET" }); + if (res.status === 401 || res.status === 403) { + this.authErrorHandler() + } if (res.status !== 200) { Toast.error({ content: Locale.Error.Network, @@ -23,15 +26,8 @@ export class ClientApi { throw new Error(Locale.Error.Network); } const resJson = await res.json(); - if (resJson.code === 401) { - Toast.error({ - content: Locale.Auth.Expiration, - duration: 5, - theme: "light" - }); - const accessStore = useAccessStore.getState(); - accessStore.updateToken(""); - throw new Error(Locale.Auth.Expiration); + if (resJson.code === 401 || resJson.code === 403) { + this.authErrorHandler() } else if (resJson.code !== 200) { Toast.error({ content: resJson.msg, @@ -54,6 +50,9 @@ export class ClientApi { }, method: "POST" }); + if (res.status === 401 || res.status === 403) { + this.authErrorHandler() + } if (res.status !== 200) { Toast.error({ content: Locale.Error.Network, @@ -63,15 +62,8 @@ export class ClientApi { throw new Error(Locale.Error.Network); } const resJson = await res.json(); - if (resJson.code === 401) { - Toast.error({ - content: Locale.Auth.Expiration, - duration: 5, - theme: "light" - }); - const accessStore = useAccessStore.getState(); - accessStore.updateToken(""); - throw new Error(Locale.Auth.Expiration); + if (resJson.code === 401 || resJson.code === 403) { + this.authErrorHandler() } else if (resJson.code !== 200) { Toast.error({ content: resJson.msg, @@ -116,6 +108,17 @@ export class ClientApi { const proxyPath = import.meta.env.VITE_PROXY_PATH; return [proxyPath, path].join(""); } + + authErrorHandler(): void { + Toast.error({ + content: Locale.Auth.Expiration, + duration: 5, + theme: "light" + }); + const accessStore = useAccessStore.getState(); + accessStore.updateToken(""); + throw new Error(Locale.Auth.Expiration); + } } export const api = new ClientApi();