diff --git a/modules/frontend/src/api/AuthClient/AuthClient.ts b/modules/frontend/src/api/AuthClient/AuthClient.ts index b71f5e7..2619db9 100644 --- a/modules/frontend/src/api/AuthClient/AuthClient.ts +++ b/modules/frontend/src/api/AuthClient/AuthClient.ts @@ -10,15 +10,13 @@ async function getRefreshed(): Promise { if (!refreshPromise) { refreshPromise = (async () => { try { - const res = await refreshTokens(); - // consider refresh successful if res.data exists + const res = await refreshTokens({ throwOnError: true }); return !!res.data; - } catch (err) { - return false; // failed to refresh + } catch { + return false; } })(); } - return refreshPromise; } @@ -27,29 +25,33 @@ const baseClient = createClient(createConfig({ baseUrl: '/api/v1 export const authClient: Client = { ...baseClient, - request: function < + // Force the function to match the expected signature exactly + request: (async < TData = unknown, TError = unknown, - ThrowOnError extends boolean = true, + ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = 'fields', >( options: Omit, 'method'> & Pick>, 'method'> - ): RequestResult { + ): Promise> => { + + // Initial request + let result = await baseClient.request(options); - // Wrap logic inside a Promise to satisfy RequestResult type - return baseClient.request(options).catch(async (err: any) => { - if (err?.status === 401) { - const refreshed = await getRefreshed(); - if (!refreshed) { - localStorage.clear(); - window.location.href = "/login"; - throw err; - } - // Retry original request - return baseClient.request(options); + // Check for 401 + if ((result as any)?.error?.response?.status === 401) { + const refreshed = await getRefreshed(); + + if (refreshed) { + // Retry + result = await baseClient.request(options); + } else { + localStorage.clear(); + window.location.href = "/login"; } - throw err; - }) as RequestResult; - }, -}; + } + + return result as RequestResult; + }) as Client['request'], // Cast the function itself to match the Client interface +}; \ No newline at end of file