Better support for environement variables

This commit is contained in:
Joseph Garrone 2024-03-02 10:04:28 +01:00
parent cb5685dd41
commit fd4f8113ef
6 changed files with 25 additions and 12 deletions

View file

@ -20,7 +20,7 @@
"keywords": [], "keywords": [],
"dependencies": { "dependencies": {
"evt": "^2.5.7", "evt": "^2.5.7",
"keycloakify": "^9.5.6", "keycloakify": "^9.5.8",
"oidc-spa": "^4.2.1", "oidc-spa": "^4.2.1",
"powerhooks": "^1.0.8", "powerhooks": "^1.0.8",
"react": "^18.2.0", "react": "^18.2.0",

View file

@ -10,7 +10,10 @@ export const { getKcContext } = createGetKcContext<KcContextExtension>({
pageId: "my-extra-page-2.ftl", pageId: "my-extra-page-2.ftl",
someCustomValue: "foo bar" someCustomValue: "foo bar"
} }
] ],
mockProperties: {
MY_ENV_VARIABLE: "Mocked value"
}
}); });
export const { kcContext } = getKcContext({ export const { kcContext } = getKcContext({

View file

@ -1,6 +1,6 @@
// Copy pasted from: https://github.com/InseeFrLab/keycloakify/blob/main/src/login/Template.tsx // Copy pasted from: https://github.com/InseeFrLab/keycloakify/blob/main/src/login/Template.tsx
import { useState } from "react"; import { useState, useEffect } from "react";
import { assert } from "keycloakify/tools/assert"; import { assert } from "keycloakify/tools/assert";
import { clsx } from "keycloakify/tools/clsx"; import { clsx } from "keycloakify/tools/clsx";
import { usePrepareTemplate } from "keycloakify/lib/usePrepareTemplate"; import { usePrepareTemplate } from "keycloakify/lib/usePrepareTemplate";
@ -47,6 +47,10 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
useState(()=> { document.title = i18n.msgStr("loginTitle", kcContext.realm.displayName); }); useState(()=> { document.title = i18n.msgStr("loginTitle", kcContext.realm.displayName); });
useEffect(() => {
console.log(`Value of MY_ENV_VARIABLE on the Keycloak server: "${kcContext.properties.MY_ENV_VARIABLE}"`);
}, []);
if (!isReady) { if (!isReady) {
return null; return null;
} }

View file

@ -1,8 +1,7 @@
import { createGetKcContext } from "keycloakify/login"; import { createGetKcContext } from "keycloakify/login";
export type KcContextExtension = export type KcContextExtension =
// WARNING: It's important to keep in sync the extraThemeProperties declared in the package.json and this type definition. | { pageId: "login.ftl"; }
| { pageId: "login.ftl"; extraThemeProperties: { foo: string; }; }
| { pageId: "my-extra-page-1.ftl"; } | { pageId: "my-extra-page-1.ftl"; }
| { pageId: "my-extra-page-2.ftl"; someCustomValue: string; } | { pageId: "my-extra-page-2.ftl"; someCustomValue: string; }
// NOTE: register.ftl is deprecated in favor of register-user-profile.ftl // NOTE: register.ftl is deprecated in favor of register-user-profile.ftl
@ -30,7 +29,7 @@ export const { getKcContext } = createGetKcContext<KcContextExtension>({
}, },
{ {
pageId: "my-extra-page-2.ftl", pageId: "my-extra-page-2.ftl",
someCustomValue: "foo bar baz" someCustomValue: "foo bar baz",
}, },
{ {
//NOTE: You will either use register.ftl (legacy) or register-user-profile.ftl, not both //NOTE: You will either use register.ftl (legacy) or register-user-profile.ftl, not both
@ -88,7 +87,12 @@ export const { getKcContext } = createGetKcContext<KcContextExtension>({
}, },
} }
] ],
// Defined in vite.config.ts
// See: https://docs.keycloakify.dev/environnement-variables
mockProperties: {
MY_ENV_VARIABLE: "Mocked value"
}
}); });
export const { kcContext } = getKcContext({ export const { kcContext } = getKcContext({

View file

@ -12,9 +12,11 @@ export default defineConfig({
react(), react(),
commonjs(), commonjs(),
keycloakify({ keycloakify({
// See: https://docs.keycloakify.dev/build-options#themename
themeName: "keycloakify-starter", themeName: "keycloakify-starter",
// See: https://docs.keycloakify.dev/environnement-variables
extraThemeProperties: [ extraThemeProperties: [
"foo=bar" "MY_ENV_VARIABLE=${env.MY_ENV_VARIABLE:}"
], ],
// This is a hook that will be called after the build is done // This is a hook that will be called after the build is done
// but before the jar is created. // but before the jar is created.

View file

@ -5886,10 +5886,10 @@ jwt-decode@^3.1.2:
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-3.1.2.tgz#3fb319f3675a2df0c2895c8f5e9fa4b67b04ed59" resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-3.1.2.tgz#3fb319f3675a2df0c2895c8f5e9fa4b67b04ed59"
integrity sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A== integrity sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A==
keycloakify@^9.5.6: keycloakify@^9.5.8:
version "9.5.6" version "9.5.8"
resolved "https://registry.yarnpkg.com/keycloakify/-/keycloakify-9.5.6.tgz#28a43c8384cc03d0aa495a59706d2604d839d765" resolved "https://registry.yarnpkg.com/keycloakify/-/keycloakify-9.5.8.tgz#aa6b3cb12f35bd6ec022d15e45ad6b46f4a920ed"
integrity sha512-GPsc3OrYRQWG9D5HKSLriAl9+Nc7NNPwTbSPG/0il9lwzgBc675BZb8v8Dyx1H/LbIZ3K6+ftMhcdEYTyCSU1A== integrity sha512-8qPiQOQu+b+/Ql04pq8Tlv924ReBS9YJDC28IXRih/XLuM1M0tOU7L6WW/fcNpzWmXZ4X1j2vHxiPjqEoWyo1g==
dependencies: dependencies:
"@babel/generator" "^7.22.9" "@babel/generator" "^7.22.9"
"@babel/parser" "^7.22.7" "@babel/parser" "^7.22.7"