keycloakify-custom/src/keycloak-theme/KcApp.tsx

67 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-02-26 11:32:22 +00:00
import "./KcApp.css";
import { lazy, Suspense } from "react";
import type { KcContext } from "./kcContext";
2023-02-26 13:07:11 +00:00
import { useI18n } from "./i18n";
import Fallback, { defaultKcProps, type KcProps, type PageProps } from "keycloakify";
2023-02-26 11:32:22 +00:00
import Template from "./Template";
2023-02-26 15:35:55 +00:00
import DefaultTemplate from "keycloakify/lib/Template";
2023-02-26 11:32:22 +00:00
2023-02-26 15:35:55 +00:00
const Login = lazy(()=> import("./pages/Login"));
// If you can, favor register-user-profile.ftl over register.ftl, see: https://docs.keycloakify.dev/realtime-input-validation
2023-02-26 11:32:22 +00:00
const Register = lazy(() => import("./pages/Register"));
const Terms = lazy(() => import("./pages/Terms"));
const MyExtraPage1 = lazy(() => import("./pages/MyExtraPage1"));
const MyExtraPage2 = lazy(() => import("./pages/MyExtraPage2"));
2023-02-26 15:35:55 +00:00
const Info = lazy(()=> import("keycloakify/lib/pages/Info"));
2023-02-26 11:32:22 +00:00
2023-02-26 13:07:11 +00:00
// This is like editing the theme.properties
// https://github.com/keycloak/keycloak/blob/11.0.3/themes/src/main/resources/theme/keycloak/login/theme.properties
const kcProps: KcProps = {
...defaultKcProps,
// NOTE: The classes are defined in ./KcApp.css
// You can add your classes alongside thoses that are present in the default Keycloak theme...
2023-02-26 13:07:11 +00:00
"kcHtmlClass": [...defaultKcProps.kcHtmlClass, "my-root-class"],
// ...or overwrite
"kcHeaderWrapperClass": "my-color my-font"
2023-02-26 11:32:22 +00:00
};
2023-02-26 13:07:11 +00:00
export default function App(props: { kcContext: KcContext; }) {
const { kcContext } = props;
2023-02-26 11:32:22 +00:00
const i18n = useI18n({ kcContext });
//NOTE: Locales not yet downloaded
if (i18n === null) {
return null;
}
2023-02-26 13:07:11 +00:00
const pageProps: Omit<PageProps<any, typeof i18n>, "kcContext"> = {
2023-02-26 11:32:22 +00:00
i18n,
2023-02-26 15:35:55 +00:00
// Here we have overloaded the default template, however you could use the default one with:
//Template: DefaultTemplate,
2023-02-26 11:32:22 +00:00
Template,
2023-02-26 15:35:55 +00:00
// Wether or not we should download the CSS and JS resources that comes with the default Keycloak theme.
2023-02-26 13:07:11 +00:00
doFetchDefaultThemeResources: true,
...kcProps,
};
2023-02-26 11:32:22 +00:00
return (
<Suspense>
{(() => {
switch (kcContext.pageId) {
2023-02-26 13:07:11 +00:00
case "login.ftl": return <Login {...{ kcContext, ...pageProps }} />;
case "register.ftl": return <Register {...{ kcContext, ...pageProps }} />;
case "terms.ftl": return <Terms {...{ kcContext, ...pageProps }} />;
case "my-extra-page-1.ftl": return <MyExtraPage1 {...{ kcContext, ...pageProps }} />;
case "my-extra-page-2.ftl": return <MyExtraPage2 {...{ kcContext, ...pageProps }} />;
2023-02-26 15:35:55 +00:00
// We choose to use the default Template for the Info page and to download the theme resources.
case "info.ftl": return <Info {...{ kcContext, ...pageProps}} Template={DefaultTemplate} doFetchDefaultThemeResources={true} />;
2023-02-26 13:07:11 +00:00
default: return <Fallback {...{ kcContext, ...pageProps }} />;
2023-02-26 11:32:22 +00:00
}
})()}
</Suspense>
);
}