acc_invoices: вход через Keycloak (PKCE, Google, только @khbuild.pro) вместо вставки токена
All checks were successful
ci / test (push) Successful in 54s
ci / deploy (push) Successful in 3m14s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-04 14:16:11 +03:00
parent 16e9b2c085
commit 4a351306bb
7 changed files with 106 additions and 27 deletions

View File

@@ -1,8 +1,9 @@
import * as Ariakit from "@ariakit/react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { ApiError, api, getToken, setToken, type Detail, type Line, type Lookups, type Me, type Row } from "./api.js";
import { ApiError, api, getToken, type Detail, type Line, type Lookups, type Me, type Row } from "./api.js";
import { FormDialog } from "./FormDialog.js";
import { Login } from "./Login.js";
import { finishLogin, logout, relogin } from "./oidc.js";
import { day, money, shortDate } from "./format.js";
interface ListResp { items: Row[]; counts: Record<string, number> }
@@ -11,20 +12,26 @@ const hashFilter = (): string => new URLSearchParams(location.hash.slice(1)).get
export function App() {
const [authed, setAuthed] = useState(!!getToken());
const [ready, setReady] = useState(!new URLSearchParams(location.search).has("code") && !new URLSearchParams(location.search).has("error"));
useEffect(() => {
if (ready) return;
void finishLogin().then((err) => { if (err) setAuthErr(err); setAuthed(!!getToken()); setReady(true); });
}, [ready]);
const [me, setMe] = useState<Me | null>(null);
const [authErr, setAuthErr] = useState<string>();
useEffect(() => {
if (!authed) return;
if (!authed || !ready) return;
api<Me>("me").then(setMe).catch((e) => {
if (e instanceof ApiError && (e.status === 401 || e.status === 403)) { setToken(""); setAuthed(false); setAuthErr(e.message); }
if (e instanceof ApiError && (e.status === 401 || e.status === 403)) { if (e.status === 401) void relogin(); else setAuthErr(e.message); }
else setAuthErr(String(e));
});
}, [authed]);
}, [authed, ready]);
if (!authed) return <Login onDone={() => { setAuthErr(undefined); setAuthed(true); }} error={authErr} />;
if (!ready) return <div className="center">Вход…</div>;
if (!authed) return <Login error={authErr} />;
if (!me) return <div className="center">{authErr ?? "Загрузка…"}</div>;
return <Main me={me} onLogout={() => { setToken(""); setAuthed(false); setMe(null); }} />;
return <Main me={me} onLogout={() => { void logout(); setAuthed(false); setMe(null); }} />;
}
function Main({ me, onLogout }: { me: Me; onLogout: () => void }) {