Make template initialization not ejected by default
This commit is contained in:
parent
785ed095bc
commit
9f875160ea
@ -8,7 +8,7 @@ import { assert, type Equals } from "tsafe/assert";
|
|||||||
const KEYCLOAK_VERSION = {
|
const KEYCLOAK_VERSION = {
|
||||||
FOR_LOGIN_THEME: "25.0.4",
|
FOR_LOGIN_THEME: "25.0.4",
|
||||||
FOR_ACCOUNT_MULTI_PAGE: "21.1.2",
|
FOR_ACCOUNT_MULTI_PAGE: "21.1.2",
|
||||||
LAST_24: "24.0.4"
|
LAST_24: "24.0.6"
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export async function downloadKeycloakDefaultTheme(params: {
|
export async function downloadKeycloakDefaultTheme(params: {
|
||||||
|
@ -152,7 +152,6 @@ export declare namespace KcContext {
|
|||||||
authenticationSession?: {
|
authenticationSession?: {
|
||||||
authSessionId: string;
|
authSessionId: string;
|
||||||
tabId: string;
|
tabId: string;
|
||||||
ssoLoginInOtherTabsUrl: string;
|
|
||||||
};
|
};
|
||||||
properties: {};
|
properties: {};
|
||||||
"x-keycloakify": {
|
"x-keycloakify": {
|
||||||
|
@ -3,11 +3,10 @@ import { assert } from "keycloakify/tools/assert";
|
|||||||
import { clsx } from "keycloakify/tools/clsx";
|
import { clsx } from "keycloakify/tools/clsx";
|
||||||
import type { TemplateProps } from "keycloakify/login/TemplateProps";
|
import type { TemplateProps } from "keycloakify/login/TemplateProps";
|
||||||
import { getKcClsx } from "keycloakify/login/lib/kcClsx";
|
import { getKcClsx } from "keycloakify/login/lib/kcClsx";
|
||||||
import { useInsertScriptTags } from "keycloakify/tools/useInsertScriptTags";
|
|
||||||
import { useInsertLinkTags } from "keycloakify/tools/useInsertLinkTags";
|
|
||||||
import { useSetClassName } from "keycloakify/tools/useSetClassName";
|
import { useSetClassName } from "keycloakify/tools/useSetClassName";
|
||||||
import type { I18n } from "./i18n";
|
import type { I18n } from "./i18n";
|
||||||
import type { KcContext } from "./KcContext";
|
import type { KcContext } from "./KcContext";
|
||||||
|
import { useInitTemplate } from "keycloakify/login/useInitTemplate";
|
||||||
|
|
||||||
export default function Template(props: TemplateProps<KcContext, I18n>) {
|
export default function Template(props: TemplateProps<KcContext, I18n>) {
|
||||||
const {
|
const {
|
||||||
@ -30,7 +29,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
|
|
||||||
const { msg, msgStr, getChangeLocaleUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
const { msg, msgStr, getChangeLocaleUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
||||||
|
|
||||||
const { realm, locale, auth, url, message, isAppInitiatedAction, authenticationSession, scripts } = kcContext;
|
const { realm, locale, auth, url, message, isAppInitiatedAction } = kcContext;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
document.title = documentTitle ?? msgStr("loginTitle", kcContext.realm.displayName);
|
document.title = documentTitle ?? msgStr("loginTitle", kcContext.realm.displayName);
|
||||||
@ -46,71 +45,9 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
className: bodyClassName ?? kcClsx("kcBodyClass")
|
className: bodyClassName ?? kcClsx("kcBodyClass")
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
const { isReadyToRender } = useInitTemplate({ kcContext, doUseDefaultCss });
|
||||||
const { currentLanguageTag } = locale ?? {};
|
|
||||||
|
|
||||||
if (currentLanguageTag === undefined) {
|
if (!isReadyToRender) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const html = document.querySelector("html");
|
|
||||||
assert(html !== null);
|
|
||||||
html.lang = currentLanguageTag;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const { areAllStyleSheetsLoaded } = useInsertLinkTags({
|
|
||||||
componentOrHookName: "Template",
|
|
||||||
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({
|
|
||||||
componentOrHookName: "Template",
|
|
||||||
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 null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
99
src/login/useInitTemplate.ts
Normal file
99
src/login/useInitTemplate.ts
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
import { useEffect } from "react";
|
||||||
|
import { assert } from "keycloakify/tools/assert";
|
||||||
|
import { useInsertScriptTags } from "keycloakify/tools/useInsertScriptTags";
|
||||||
|
import { useInsertLinkTags } from "keycloakify/tools/useInsertLinkTags";
|
||||||
|
import { KcContext } from "keycloakify/login/KcContext/KcContext";
|
||||||
|
|
||||||
|
export type KcContextLike = {
|
||||||
|
url: {
|
||||||
|
resourcesCommonPath: string;
|
||||||
|
resourcesPath: string;
|
||||||
|
ssoLoginInOtherTabsUrl: string;
|
||||||
|
};
|
||||||
|
locale?: {
|
||||||
|
currentLanguageTag: string;
|
||||||
|
};
|
||||||
|
scripts: string[];
|
||||||
|
authenticationSession?: {
|
||||||
|
authSessionId: string;
|
||||||
|
tabId: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
assert<keyof KcContextLike extends keyof KcContext ? true : false>();
|
||||||
|
assert<KcContext extends KcContextLike ? true : false>();
|
||||||
|
|
||||||
|
export function useInitTemplate(params: {
|
||||||
|
kcContext: KcContextLike;
|
||||||
|
doUseDefaultCss: boolean;
|
||||||
|
}) {
|
||||||
|
const { kcContext, doUseDefaultCss } = params;
|
||||||
|
|
||||||
|
const { url, locale, scripts, authenticationSession } = kcContext;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const { currentLanguageTag } = locale ?? {};
|
||||||
|
|
||||||
|
if (currentLanguageTag === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const html = document.querySelector("html");
|
||||||
|
assert(html !== null);
|
||||||
|
html.lang = currentLanguageTag;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const { areAllStyleSheetsLoaded } = useInsertLinkTags({
|
||||||
|
componentOrHookName: "Template",
|
||||||
|
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({
|
||||||
|
componentOrHookName: "Template",
|
||||||
|
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]);
|
||||||
|
|
||||||
|
return { isReadyToRender: areAllStyleSheetsLoaded };
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user