small refactor to set realm from path (#282)
This commit is contained in:
parent
516c51ee05
commit
e6fa093925
1 changed files with 27 additions and 9 deletions
36
src/App.tsx
36
src/App.tsx
|
@ -19,6 +19,7 @@ import { routes, RouteDef } from "./route-config";
|
||||||
import { PageBreadCrumbs } from "./components/bread-crumb/PageBreadCrumbs";
|
import { PageBreadCrumbs } from "./components/bread-crumb/PageBreadCrumbs";
|
||||||
import { ForbiddenSection } from "./ForbiddenSection";
|
import { ForbiddenSection } from "./ForbiddenSection";
|
||||||
import { useRealm } from "./context/realm-context/RealmContext";
|
import { useRealm } from "./context/realm-context/RealmContext";
|
||||||
|
import { useAdminClient, useFetch } from "./context/auth/AdminClient";
|
||||||
|
|
||||||
// This must match the id given as scrollableSelector in scroll-form
|
// This must match the id given as scrollableSelector in scroll-form
|
||||||
const mainPageContentId = "kc-main-content-page-container";
|
const mainPageContentId = "kc-main-content-page-container";
|
||||||
|
@ -33,19 +34,32 @@ const AppContexts = ({ children }: { children: ReactNode }) => (
|
||||||
</AccessContextProvider>
|
</AccessContextProvider>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// set the realm form the path if it's one of the know realms
|
||||||
|
const RealmPathSelector = ({ children }: { children: ReactNode }) => {
|
||||||
|
const { setRealm } = useRealm();
|
||||||
|
const { realm } = useParams<{ realm: string }>();
|
||||||
|
const adminClient = useAdminClient();
|
||||||
|
useEffect(
|
||||||
|
() =>
|
||||||
|
useFetch(
|
||||||
|
() => adminClient.realms.find(),
|
||||||
|
(realms) => {
|
||||||
|
if (realms.findIndex((r) => r.realm == realm) !== -1) {
|
||||||
|
setRealm(realm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
return <>{children}</>;
|
||||||
|
};
|
||||||
|
|
||||||
// If someone tries to go directly to a route they don't
|
// If someone tries to go directly to a route they don't
|
||||||
// have access to, show forbidden page.
|
// have access to, show forbidden page.
|
||||||
type SecuredRouteProps = { route: RouteDef };
|
type SecuredRouteProps = { route: RouteDef };
|
||||||
const SecuredRoute = ({ route }: SecuredRouteProps) => {
|
const SecuredRoute = ({ route }: SecuredRouteProps) => {
|
||||||
const { setRealm } = useRealm();
|
|
||||||
const { realm } = useParams<{ realm: string }>();
|
|
||||||
useEffect(() => {
|
|
||||||
if (realm) {
|
|
||||||
setRealm(realm);
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
const { hasAccess } = useAccess();
|
const { hasAccess } = useAccess();
|
||||||
|
|
||||||
if (hasAccess(route.access)) return <route.component />;
|
if (hasAccess(route.access)) return <route.component />;
|
||||||
|
|
||||||
return <ForbiddenSection />;
|
return <ForbiddenSection />;
|
||||||
|
@ -68,7 +82,11 @@ export const App = () => {
|
||||||
exact
|
exact
|
||||||
key={i}
|
key={i}
|
||||||
path={route.path}
|
path={route.path}
|
||||||
component={() => <SecuredRoute route={route} />}
|
component={() => (
|
||||||
|
<RealmPathSelector>
|
||||||
|
<SecuredRoute route={route} />
|
||||||
|
</RealmPathSelector>
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</Switch>
|
</Switch>
|
||||||
|
|
Loading…
Reference in a new issue