diff --git a/web/src/domain/components/icon.ts b/web/src/domain/components/icon.ts index 3bf9493b..a8e887f5 100644 --- a/web/src/domain/components/icon.ts +++ b/web/src/domain/components/icon.ts @@ -2,6 +2,7 @@ * Size of an icon. */ export type IconSize = + | "xx-small" | "x-small" | "small" | "medium" diff --git a/web/src/lib/clients/ecomap/http.ts b/web/src/lib/clients/ecomap/http.ts index 3e0d5095..836c224d 100644 --- a/web/src/lib/clients/ecomap/http.ts +++ b/web/src/lib/clients/ecomap/http.ts @@ -4,6 +4,12 @@ import type { components, paths } from "../../../../api/ecomap/http"; import { clearToken, getToken } from "../../utils/auth"; import { CommonRoutes } from "../../../routes/constants/routes"; +/** + * Endpoints that should be ignored if the status code from the server response is 401. + * Used to prevent the employee token from being deleted and the employee from being redirected to the sign in page. + */ +const UNAUTHORIZED_IGNORED_ENDPOINTS: (keyof paths)[] = ["/employees/password"]; + const middleware: Middleware = { onRequest(request) { const token = getToken(); @@ -13,17 +19,31 @@ const middleware: Middleware = { return request; }, - async onResponse(response) { - let body = await response.text(); + async onResponse(response, options) { + let body = (await response.text()) || null; switch (response.status) { - case 401: + case 401: { + const responseUrl = new URL(response.url); + + // Check if it's an endpoint that should be ignored. + const isIgnoredEndpoint = UNAUTHORIZED_IGNORED_ENDPOINTS.some( + endpoint => { + const pathname = `${options.baseUrl}${endpoint}`; + return pathname === responseUrl.pathname; + }, + ); + if (isIgnoredEndpoint) { + break; + } + clearToken(); // Only redirect if the page is not the sign in page. if (location.pathname !== CommonRoutes.SIGN_IN) { navigate(CommonRoutes.SIGN_IN); } break; + } case 403: navigate(CommonRoutes.FORBIDDEN, { replace: true }); @@ -44,6 +64,7 @@ const middleware: Middleware = { const ecomapHttpClient = createClient({ baseUrl: "/api", }); + ecomapHttpClient.use(middleware); export default ecomapHttpClient; diff --git a/web/src/lib/components/FormControl.svelte b/web/src/lib/components/FormControl.svelte index c6a4aa6e..88fc9906 100644 --- a/web/src/lib/components/FormControl.svelte +++ b/web/src/lib/components/FormControl.svelte @@ -1,4 +1,6 @@