Skip to content

Latest commit

 

History

History
74 lines (54 loc) · 2.36 KB

taking-ownership-of-the-script-responsible-for-generating-the-kccontext.md

File metadata and controls

74 lines (54 loc) · 2.36 KB

🤓 Taking ownership of the kcContext

This documentation explore how to finely controlls what is and isnt included in the window.kcContext object.

{% hint style="info" %} If you simply want to remove some specific values from the kcContext you can use the kcContextExclusionFtl option. {% endhint %}

Some values, like for example the realm attributes (kcContext.realm.attributes) are explicitely excluded from the KcContext.

In the following video we explore how to include them back.

{% embed url="https://www.youtube.com/watch?v=WdSPrpFObhg" %}

Note that in the video we includes all the realm attributes. We might want to expose only a specific set of values. For this we could do:

{% code title="node_modules/keycloakify/src/bin/keycloakify/generateFtl/kcContextDeclarationTemplate.ftl" %}

-    ) || (
-        key == "attributes" &&
-        areSamePath(path, ["realm"])
-    ) || (
+    ) || (
+        areSamePath(path, ["realm", "attributes"]) &&
+        !["myFirstAttribute", "mySecondAttribute"]?seq_contains(key)
+    ) || (

{% endcode %}

We could also chose to include only the realm attributes with a specific prefix, for example theme_:

{% code title="node_modules/keycloakify/src/bin/keycloakify/generateFtl/kcContextDeclarationTemplate.ftl" %}

-    ) || (
-        key == "attributes" &&
-        areSamePath(path, ["realm"])
-    ) || (
+    ) || (
+        areSamePath(path, ["realm", "attributes"]) &&
+        !key?starts_with("theme_")
+    ) || (

{% endcode %}

Setting up patch-package

As explained in the video:

Add patch-package add dev dependency

yarn add --dev patch-package

Edit the FreeMarker template that generates the KcContext in:

node_modules/keycloakify/src/bin/keycloakify/generateFtl/kcContextDeclarationTemplate.ftl

You can then create a diff for your changes by running:

npx patch-package keycloakify

Then add a postinstall script to your package.json:

{
    "name": "keycloakify-starter",
    "scripts": {
        "postinstall": "patch-package",
        "dev": "vite",

Commit the patch/ directory that have been created by patch-package.