Compare commits
2 commits
2a47e64386
...
4c8ed09429
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c8ed09429 | |||
| b572a6b20c |
2 changed files with 62 additions and 1 deletions
55
modules/frontend/src/api/AuthClient/AuthClient.ts
Normal file
55
modules/frontend/src/api/AuthClient/AuthClient.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import { createClient, createConfig } from "../client";
|
||||||
|
import type { ClientOptions as ClientOptions2 } from '../types.gen';
|
||||||
|
import type { Client, RequestOptions, RequestResult } from "../client";
|
||||||
|
import { refreshTokens } from "../../auth";
|
||||||
|
import type { ResponseStyle } from "../client";
|
||||||
|
|
||||||
|
let refreshPromise: Promise<boolean> | null = null;
|
||||||
|
|
||||||
|
async function getRefreshed(): Promise<boolean> {
|
||||||
|
if (!refreshPromise) {
|
||||||
|
refreshPromise = (async () => {
|
||||||
|
try {
|
||||||
|
const res = await refreshTokens();
|
||||||
|
// consider refresh successful if res.data exists
|
||||||
|
return !!res.data;
|
||||||
|
} catch (err) {
|
||||||
|
return false; // failed to refresh
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
|
return refreshPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseClient = createClient(createConfig<ClientOptions2>({ baseUrl: '/api/v1' }));
|
||||||
|
|
||||||
|
export const authClient: Client = {
|
||||||
|
...baseClient,
|
||||||
|
|
||||||
|
request: function <
|
||||||
|
TData = unknown,
|
||||||
|
TError = unknown,
|
||||||
|
ThrowOnError extends boolean = false,
|
||||||
|
TResponseStyle extends ResponseStyle = 'fields',
|
||||||
|
>(
|
||||||
|
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'> &
|
||||||
|
Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, 'method'>
|
||||||
|
): RequestResult<TData, TError, ThrowOnError, TResponseStyle> {
|
||||||
|
|
||||||
|
// Wrap logic inside a Promise to satisfy RequestResult type
|
||||||
|
return baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(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<TData, TError, ThrowOnError, TResponseStyle>(options);
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}) as RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
@ -9,6 +9,7 @@ import { getTitles, type CursorObj, type Title, type TitleSort } from "../../api
|
||||||
import { LayoutSwitch } from "../../components/LayoutSwitch/LayoutSwitch";
|
import { LayoutSwitch } from "../../components/LayoutSwitch/LayoutSwitch";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { type TitlesFilter, TitlesFilterPanel } from "../../components/TitlesFilterPanel/TitlesFilterPanel";
|
import { type TitlesFilter, TitlesFilterPanel } from "../../components/TitlesFilterPanel/TitlesFilterPanel";
|
||||||
|
import { authClient } from "../../api/AuthClient/AuthClient";
|
||||||
|
|
||||||
const PAGE_SIZE = 10;
|
const PAGE_SIZE = 10;
|
||||||
|
|
||||||
|
|
@ -51,7 +52,12 @@ export default function TitlesPage() {
|
||||||
offset: PAGE_SIZE,
|
offset: PAGE_SIZE,
|
||||||
fields: "all",
|
fields: "all",
|
||||||
},
|
},
|
||||||
});
|
client: authClient,
|
||||||
|
},);
|
||||||
|
|
||||||
|
if (response.response.status === 403) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
items: response.data?.data ?? [],
|
items: response.data?.data ?? [],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue