Skip to content

Commit

Permalink
feat(upgrade): upgrade keycloakify to v11
Browse files Browse the repository at this point in the history
  • Loading branch information
bivanalhar committed Jan 16, 2025
1 parent e0b88ab commit a470086
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 88 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@emotion/react": "^11.13.5",
"@emotion/styled": "^11.13.5",
"framer-motion": "^11.2.0",
"keycloakify": "^10.1.6",
"keycloakify": "^11",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Expand Down
52 changes: 24 additions & 28 deletions src/kc.gen.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* prettier-ignore-start */
// This file is auto-generated by the `update-kc-gen` command. Do not edit it manually.
// Hash: 9ae0fc6db02c7aaca5511c810584dc11912602c9a6a8c8f778a301e8e52807fc

/* eslint-disable */

// @ts-nocheck

// noinspection JSUnusedGlobalSymbols

// This file is auto-generated by Keycloakify

import { lazy, Suspense, type ReactNode } from "react";

export type ThemeName = "coursemology-keycloakify";
Expand All @@ -19,37 +18,34 @@ export type KcEnvName = "MY_ENV_VARIABLE";
export const kcEnvNames: KcEnvName[] = ["MY_ENV_VARIABLE"];

export const kcEnvDefaults: Record<KcEnvName, string> = {
"MY_ENV_VARIABLE": ""
MY_ENV_VARIABLE: "",
};

export type KcContext =
| import("./login/KcContext").KcContext
;
/**
* NOTE: Do not import this type except maybe in your entrypoint.
* If you need to import the KcContext import it either from src/login/KcContext.ts or src/account/KcContext.ts.
* Depending on the theme type you are working on.
*/
export type KcContext = import("./login/KcContext").KcContext;

declare global {
interface Window {
kcContext?: KcContext;
}
interface Window {
kcContext?: KcContext;
}
}

export const KcLoginPage = lazy(() => import("./login/KcPage"));

export function KcPage(
props: {
kcContext: KcContext;
fallback?: ReactNode;
}
) {
const { kcContext, fallback } = props;
return (
<Suspense fallback={fallback}>
{(() => {
switch (kcContext.themeType) {
case "login": return <KcLoginPage kcContext={kcContext} />;
}
})()}
</Suspense>
);
export function KcPage(props: { kcContext: KcContext; fallback?: ReactNode }) {
const { kcContext, fallback } = props;
return (
<Suspense fallback={fallback}>
{(() => {
switch (kcContext.themeType) {
case "login":
return <KcLoginPage kcContext={kcContext} />;
}
})()}
</Suspense>
);
}

/* prettier-ignore-end */
11 changes: 5 additions & 6 deletions src/login/Template.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Original file: https://github.com/InseeFrLab/keycloakify/blob/main/src/login/Template.tsx

import { kcSanitize } from "keycloakify/lib/kcSanitize";
import { type TemplateProps as KeycloakTemplateProps } from "keycloakify/login/TemplateProps";

import AlertBox from "src/lib/components/core/AlertBox";
Expand All @@ -21,9 +22,9 @@ const Template = (props: KeycloakTemplateProps<KcContext, I18n>) => {
children,
} = props;

const { msg } = i18n;
const { msg, enabledLanguages } = i18n;

const { realm, locale, message, isAppInitiatedAction } = kcContext;
const { realm, message, isAppInitiatedAction } = kcContext;
const { homeUrl } = useCoursemologyUrls();

return (
Expand All @@ -33,9 +34,7 @@ const Template = (props: KeycloakTemplateProps<KcContext, I18n>) => {
textNode={msg("loginTitleHtml", realm.displayNameHtml)}
brandUrl={homeUrl}
/>
{realm.internationalizationEnabled && (
<LocaleDropdown locale={locale} i18n={i18n} />
)}
{enabledLanguages.length > 1 && <LocaleDropdown i18n={i18n} />}
</header>

<div className="relative h-full">
Expand All @@ -48,7 +47,7 @@ const Template = (props: KeycloakTemplateProps<KcContext, I18n>) => {
<AlertBox status={message.type}>
<span
dangerouslySetInnerHTML={{
__html: message.summary,
__html: kcSanitize(message.summary),
}}
/>
</AlertBox>
Expand Down
24 changes: 9 additions & 15 deletions src/login/components/LocaleDropdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
import { ChevronDownIcon } from "@chakra-ui/icons";
import { Button, Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";
import { assert } from "keycloakify/tools/assert";

import type { I18n } from "../../i18n";
import type { KcContext } from "../../KcContext";

interface LocaleDropdownProps {
locale: KcContext["locale"];
i18n: I18n;
}

const LocaleDropdown = (props: LocaleDropdownProps): JSX.Element | null => {
const { locale, i18n } = props;
const {
getChangeLocaleUrl,
labelBySupportedLanguageTag,
currentLanguageTag,
} = i18n;
const { i18n } = props;
const { currentLanguage, enabledLanguages } = i18n;

return (assert(locale !== undefined), true) && locale.supported.length > 1 ? (
return (
<div>
<Menu>
<MenuButton as={Button} rightIcon={<ChevronDownIcon />}>
{labelBySupportedLanguageTag[currentLanguageTag]}
{currentLanguage.label}
</MenuButton>
<MenuList>
{locale.supported.map(({ languageTag }) => (
{enabledLanguages.map(({ languageTag, label, href }, i) => (
<MenuItem
id={`language-${i + 1}`}
as="a"
href={getChangeLocaleUrl(languageTag)}
href={href}
key={languageTag}
>
{labelBySupportedLanguageTag[languageTag]}
{label}
</MenuItem>
))}
</MenuList>
</Menu>
</div>
) : null;
);
};

export default LocaleDropdown;
70 changes: 38 additions & 32 deletions src/login/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import { createUseI18n } from "keycloakify/login";
import { i18nBuilder } from "keycloakify/login";
import type { ThemeName } from "src/kc.gen";

export const { useI18n, ofTypeI18n } = createUseI18n({
// NOTE: Here you can override the default i18n messages
// or define new ones that, for example, you would have
// defined in the Keycloak admin UI for UserProfile
// https://user-images.githubusercontent.com/6702424/182050652-522b6fe6-8ee5-49df-aca3-dba2d33f24a5.png
en: {
// Here we overwrite the default english value for the message "doForgotPassword"
// that is "Forgot Password?" see: https://github.com/InseeFrLab/keycloakify/blob/f0ae5ea908e0aa42391af323b6d5e2fd371af851/src/lib/i18n/generated_messages/18.0.1/login/en.ts#L17
doRegister: "Sign up",
dontYetHaveAnAccount: "Don't yet have an account?",
forgotPassword: "Forgot Password",
rememberMe: "Remember me on this device",
resendConfirmationEmail: "Resend confirmation email",
troubleSigningIn: "Trouble signing in?",
usernameOrEmail: "Email address",
emailNotVerified:
"Your email account has not been verified. Please verify your email before proceeding.",
},
"zh-CN": {
doRegister: "注册",
dontYetHaveAnAccount: "还没有账户?",
forgotPassword: "忘记密码",
rememberMe: "在这台设备上记住我",
resendConfirmationEmail: "重新发送确认邮件",
troubleSigningIn: "登录遇到问题?",
usernameOrEmail: "电子邮箱地址",
emailNotVerified:
"您的电子邮件帐户尚未经过验证。 请在继续之前验证您的电子邮件。",
},
});
const { useI18n, ofTypeI18n } = i18nBuilder
.withThemeName<ThemeName>()
.withCustomTranslations({
// NOTE: Here you can override the default i18n messages
// or define new ones that, for example, you would have
// defined in the Keycloak admin UI for UserProfile
// https://user-images.githubusercontent.com/6702424/182050652-522b6fe6-8ee5-49df-aca3-dba2d33f24a5.png
en: {
// Here we overwrite the default english value for the message "doForgotPassword"
// that is "Forgot Password?" see: https://github.com/InseeFrLab/keycloakify/blob/f0ae5ea908e0aa42391af323b6d5e2fd371af851/src/lib/i18n/generated_messages/18.0.1/login/en.ts#L17
doRegister: "Sign up",
dontYetHaveAnAccount: "Don't yet have an account?",
forgotPassword: "Forgot Password",
rememberMe: "Remember me on this device",
resendConfirmationEmail: "Resend confirmation email",
troubleSigningIn: "Trouble signing in?",
usernameOrEmail: "Email address",
emailNotVerified:
"Your email account has not been verified. Please verify your email before proceeding.",
},
"zh-CN": {
doRegister: "注册",
dontYetHaveAnAccount: "还没有账户?",
forgotPassword: "忘记密码",
rememberMe: "在这台设备上记住我",
resendConfirmationEmail: "重新发送确认邮件",
troubleSigningIn: "登录遇到问题?",
usernameOrEmail: "电子邮箱地址",
emailNotVerified:
"您的电子邮件帐户尚未经过验证。 请在继续之前验证您的电子邮件。",
},
})
.build();

export type I18n = typeof ofTypeI18n;
type I18n = typeof ofTypeI18n;

export { type I18n, useI18n };
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2308,12 +2308,12 @@ json5@^2.2.2, json5@^2.2.3:
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==

keycloakify@^10.1.6:
version "10.1.6"
resolved "https://registry.yarnpkg.com/keycloakify/-/keycloakify-10.1.6.tgz#f948c0167b2fa64b851c99c2dac029fff496b1a0"
integrity sha512-yuCOM2xHxph8mmxWdK7BmsvkDfo42YOO2oqY0U94MGaBuYaSUqmUQC9dednvyApx2IndzBhd9C18H/86wvIx6Q==
keycloakify@^11:
version "11.8.9"
resolved "https://registry.yarnpkg.com/keycloakify/-/keycloakify-11.8.9.tgz#0e603e544e37c4dafac518f506c7200103c9077a"
integrity sha512-puYkK+4VfkmDfcfT4XkDAEUQfoObAon2+L+8YchBCvcGFcL8c1JkF8S1lLHX9AMuFWX1FW0r7Ye7zGJIpvcz1w==
dependencies:
tsafe "^1.6.6"
tsafe "^1.8.5"

keyv@^4.5.3:
version "4.5.4"
Expand Down Expand Up @@ -3172,7 +3172,7 @@ ts-interface-checker@^0.1.9:
resolved "https://registry.yarnpkg.com/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz#784fd3d679722bc103b1b4b8030bcddb5db2a699"
integrity sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==

tsafe@^1.6.6:
tsafe@^1.8.5:
version "1.8.5"
resolved "https://registry.yarnpkg.com/tsafe/-/tsafe-1.8.5.tgz#cdf9fa3111974ac480d7ee519f8241815e5d22ea"
integrity sha512-LFWTWQrW6rwSY+IBNFl2ridGfUzVsPwrZ26T4KUJww/py8rzaQ/SY+MIz6YROozpUCaRcuISqagmlwub9YT9kw==
Expand Down

0 comments on commit a470086

Please sign in to comment.