diff --git a/src/api/client.ts b/src/api/client.ts index 273c277..be16398 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -18,6 +18,34 @@ async function request( } const res = await fetch(`${BASE}${url}`, { ...options, headers }); + + if (res.status === 401 && withAuth) { + const refreshToken = useAuthStore.getState().refreshToken; + if (refreshToken) { + const refreshRes = await fetch(`${BASE}/web/refresh`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ refresh_token: refreshToken }), + }); + if (refreshRes.ok) { + const data = await refreshRes.json(); + if (data.ok) { + useAuthStore.getState().setAuth( + data.access_token, + data.refresh_token, + useAuthStore.getState().user! + ); + headers["Authorization"] = `Bearer ${data.access_token}`; + const retryRes = await fetch(`${BASE}${url}`, { ...options, headers }); + return retryRes.json(); + } + } + } + useAuthStore.getState().logout(); + window.location.href = "/login"; + return { ok: false, error: "Session expired" }; + } + return res.json(); } diff --git a/src/pages/dashboard/Keys.tsx b/src/pages/dashboard/Keys.tsx index dcc470d..0b59d70 100644 --- a/src/pages/dashboard/Keys.tsx +++ b/src/pages/dashboard/Keys.tsx @@ -11,19 +11,23 @@ export default function Keys() { const [qrKey, setQrKey] = useState(null); const [renaming, setRenaming] = useState(null); const [newName, setNewName] = useState(""); + const [actionError, setActionError] = useState(null); const load = () => api.action("profile").then((r) => { if (r.ok) setProfile(r); }); useEffect(() => { load(); }, []); async function handleRename(clientId: string) { if (!newName.trim()) return; - await api.action("rename_key", { client_id: clientId, name: newName.trim() }); - setRenaming(null); setNewName(""); + const res = await api.action("rename_key", { client_id: clientId, name: newName.trim() }); + if (!res.ok) { setActionError(res.error || "Ошибка переименования"); return; } + setRenaming(null); setNewName(""); setActionError(null); load(); } async function handleFreeze(clientId: string, frozen: boolean) { - await api.action("freeze_key", { client_id: clientId, frozen }); + const res = await api.action("freeze_key", { client_id: clientId, frozen }); + if (!res.ok) { setActionError(res.error || "Ошибка"); return; } + setActionError(null); load(); } @@ -33,6 +37,11 @@ export default function Keys() { {qrKey && setQrKey(null)} />}

Мои ключи

+ {actionError && ( +
+ {actionError} +
+ )} {!keys.length &&
Ключей нет. Купите подписку.
} {keys.map((k: any) => (