import { useEffect } from "react"; import { assert } from "keycloakify/tools/assert"; import { clsx } from "keycloakify/tools/clsx"; import { type TemplateProps } from "keycloakify/login/TemplateProps"; import { useGetClassName } from "keycloakify/login/lib/useGetClassName"; import { createUseInsertScriptTags } from "keycloakify/tools/useInsertScriptTags"; import { createUseInsertLinkTags } from "keycloakify/tools/useInsertLinkTags"; import { useSetClassName } from "keycloakify/tools/useSetClassName"; import type { KcContext } from "./kcContext"; import type { I18n } from "./i18n"; const { useInsertLinkTags } = createUseInsertLinkTags(); const { useInsertScriptTags } = createUseInsertScriptTags(); export default function Template(props: TemplateProps) { const { displayInfo = false, displayMessage = true, displayRequiredFields = false, headerNode, showUsernameNode = null, socialProvidersNode = null, infoNode = null, documentTitle, bodyClassName, kcContext, i18n, doUseDefaultCss, classes, children } = props; const { getClassName } = useGetClassName({ doUseDefaultCss, classes }); const { msg, msgStr, getChangeLocalUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n; const { realm, locale, auth, url, message, isAppInitiatedAction, authenticationSession, scripts } = kcContext; useEffect(() => { document.title = documentTitle ?? msgStr("loginTitle", kcContext.realm.displayName); }, []); useSetClassName({ "qualifiedName": "html", "className": getClassName("kcHtmlClass") }); useSetClassName({ "qualifiedName": "body", "className": bodyClassName ?? getClassName("kcBodyClass") }); useEffect(() => { const { currentLanguageTag } = locale ?? {}; if (currentLanguageTag === undefined) { return; } const html = document.querySelector("html"); assert(html !== null); html.lang = currentLanguageTag; }, []); const { areAllStyleSheetsLoaded } = useInsertLinkTags({ "hrefs": !doUseDefaultCss ? [] : [ `${url.resourcesCommonPath}/node_modules/@patternfly/patternfly/patternfly.min.css`, `${url.resourcesCommonPath}/node_modules/patternfly/dist/css/patternfly.min.css`, `${url.resourcesCommonPath}/node_modules/patternfly/dist/css/patternfly-additions.min.css`, `${url.resourcesCommonPath}/lib/pficon/pficon.css`, `${url.resourcesPath}/css/login.css` ] }); const { insertScriptTags } = useInsertScriptTags({ "scriptTags": [ { "type": "module", "src": `${url.resourcesPath}/js/menu-button-links.js` }, ...(authenticationSession === undefined ? [] : [ { "type": "module", "textContent": [ `import { checkCookiesAndSetTimer } from "${url.resourcesPath}/js/authChecker.js";`, ``, `checkCookiesAndSetTimer(`, ` "${authenticationSession.authSessionId}",`, ` "${authenticationSession.tabId}",`, ` "${url.ssoLoginInOtherTabsUrl}"`, `);` ].join("\n") } as const ]), ...scripts.map( script => ({ "type": "text/javascript", "src": script } as const) ) ] }); useEffect(() => { if (areAllStyleSheetsLoaded) { insertScriptTags(); } }, [areAllStyleSheetsLoaded]); if (!areAllStyleSheetsLoaded) { return null; } return (
{msg("loginTitleHtml", realm.displayNameHtml)}
{realm.internationalizationEnabled && (assert(locale !== undefined), locale.supported.length > 1) && (
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
)} {!(auth !== undefined && auth.showUsername && !auth.showResetCredentials) ? ( displayRequiredFields ? (
* {msg("requiredFields")}

{headerNode}

) : (

{headerNode}

) ) : displayRequiredFields ? (
* {msg("requiredFields")}
{showUsernameNode}
{msg("restartLoginTooltip")}
) : ( <> {showUsernameNode}
{msg("restartLoginTooltip")}
)}
{/* App-initiated actions should not see warning messages about the need to complete the action during login. */} {displayMessage && message !== undefined && (message.type !== "warning" || !isAppInitiatedAction) && (
{message.type === "success" && } {message.type === "warning" && } {message.type === "error" && } {message.type === "info" && }
)} {children} {auth !== undefined && auth.showTryAnotherWayLink && (
)} {socialProvidersNode} {displayInfo && (
{infoNode}
)}
); }