VirtWeb/virtweb_frontend/src/App.tsx
Pierre HUBERT c7de64cc02
All checks were successful
continuous-integration/drone/push Build is passing
Add API tokens support (#9)
Make it possible to create token authorized to query predetermined set of routes.

Reviewed-on: #9
Co-authored-by: Pierre HUBERT <pierre.git@communiquons.org>
Co-committed-by: Pierre HUBERT <pierre.git@communiquons.org>
2024-04-23 17:04:43 +00:00

109 lines
3.9 KiB
TypeScript

import React from "react";
import {
Route,
RouterProvider,
createBrowserRouter,
createRoutesFromElements,
} from "react-router-dom";
import "./App.css";
import { AuthApi } from "./api/AuthApi";
import { ServerApi } from "./api/ServerApi";
import {
CreateApiTokenRoute,
EditApiTokenRoute,
} from "./routes/EditAPITokenRoute";
import {
CreateNWFilterRoute,
EditNWFilterRoute,
} from "./routes/EditNWFilterRoute";
import {
CreateNetworkRoute,
EditNetworkRoute,
} from "./routes/EditNetworkRoute";
import { CreateVMRoute, EditVMRoute } from "./routes/EditVMRoute";
import { HomeRoute } from "./routes/HomeRoute";
import { IsoFilesRoute } from "./routes/IsoFilesRoute";
import { NetworkFiltersListRoute } from "./routes/NetworkFiltersListRoute";
import { NetworksListRoute } from "./routes/NetworksListRoute";
import { NotFoundRoute } from "./routes/NotFound";
import { SysInfoRoute } from "./routes/SysInfoRoute";
import { TokensListRoute } from "./routes/TokensListRoute";
import { VMListRoute } from "./routes/VMListRoute";
import { VMRoute } from "./routes/VMRoute";
import { VNCRoute } from "./routes/VNCRoute";
import { ViewApiTokenRoute } from "./routes/ViewApiTokenRoute";
import { ViewNWFilterRoute } from "./routes/ViewNWFilterRoute";
import { ViewNetworkRoute } from "./routes/ViewNetworkRoute";
import { LoginRoute } from "./routes/auth/LoginRoute";
import { OIDCCbRoute } from "./routes/auth/OIDCCbRoute";
import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
import { BaseLoginPage } from "./widgets/BaseLoginPage";
interface AuthContext {
signedIn: boolean;
setSignedIn: (signedIn: boolean) => void;
}
const AuthContextK = React.createContext<AuthContext | null>(null);
export function App() {
const [signedIn, setSignedIn] = React.useState(AuthApi.SignedIn);
const context: AuthContext = {
signedIn: signedIn,
setSignedIn: (s) => setSignedIn(s),
};
const router = createBrowserRouter(
createRoutesFromElements(
signedIn || ServerApi.Config.auth_disabled ? (
<Route path="*" element={<BaseAuthenticatedPage />}>
<Route path="" element={<HomeRoute />} />
<Route path="iso" element={<IsoFilesRoute />} />
<Route path="vms" element={<VMListRoute />} />
<Route path="vms/new" element={<CreateVMRoute />} />
<Route path="vm/:uuid" element={<VMRoute />} />
<Route path="vm/:uuid/edit" element={<EditVMRoute />} />
<Route path="vm/:uuid/vnc" element={<VNCRoute />} />
<Route path="net" element={<NetworksListRoute />} />
<Route path="net/new" element={<CreateNetworkRoute />} />
<Route path="net/:uuid" element={<ViewNetworkRoute />} />
<Route path="net/:uuid/edit" element={<EditNetworkRoute />} />
<Route path="nwfilter" element={<NetworkFiltersListRoute />} />
<Route path="nwfilter/new" element={<CreateNWFilterRoute />} />
<Route path="nwfilter/:uuid" element={<ViewNWFilterRoute />} />
<Route path="nwfilter/:uuid/edit" element={<EditNWFilterRoute />} />
<Route path="tokens" element={<TokensListRoute />} />
<Route path="token/new" element={<CreateApiTokenRoute />} />
<Route path="token/:id" element={<ViewApiTokenRoute />} />
<Route path="token/:id/edit" element={<EditApiTokenRoute />} />
<Route path="sysinfo" element={<SysInfoRoute />} />
<Route path="*" element={<NotFoundRoute />} />
</Route>
) : (
<Route path="*" element={<BaseLoginPage />}>
<Route path="" element={<LoginRoute />} />
<Route path="oidc_cb" element={<OIDCCbRoute />} />
<Route path="*" element={<NotFoundRoute />} />
</Route>
)
)
);
return (
<AuthContextK.Provider value={context}>
<RouterProvider router={router} />
</AuthContextK.Provider>
);
}
export function useAuth(): AuthContext {
return React.useContext(AuthContextK)!;
}