This is the documentation for v9, checkout the latest version
Keycloakify
HomeGitHubStartersStorybookDiscordKeycloak-js Alternative
v9
  • Keycloakify
  • Release Notes & Upgrade Instructions
  • FAQ
v9
  • 👨‍💻Quick start
  • 📥Importing your theme in Keycloak
  • 🔩Keycloakify in my App
  • 🖼️Importing assets and fonts
  • 📧Email customization
  • ✒️Terms and conditions
  • ✅Realtime input validation and custom registration fields
  • ⚠️Limitations
  • 🔧Environment Variables
  • 🌎i18n: msg(...)
  • 📖Build options
  • 💂Email domain acceptlist
  • 🛑Keycloak error in log
  • 🌉Passing values from the App to the theme
  • 💟Contributing
  • 🤔How it works
  • ⬆️Migration guides
    • ⬆️CRA -> Vite
    • ⬆️v8 -> v9
    • ⬆️v7 -> v8
    • ⬆️v6 -> v7
    • ⬆️v6.x -> v6.12
    • ⬆️v5 -> v6
Powered by GitBook
On this page

Was this helpful?

Environment Variables

PreviousLimitationsNexti18n: msg(...)

Last updated 27 days ago

Was this helpful?

Environment variables defined on the Keycloak server can be transferred to the theme. This allows for a degree of theme customization without necessitating a rebuild. This approach is particularly useful if multiple parties are reusing your theme. As an example, you can distribute a single .jar file to multiple customers, enabling them to modify certain aspect of the login page by defining specific environment variables.

Concretely the ide is to be able, if you start your Keycloak server like this:

docker run \
    -e KEYCLOAK_ADMIN=admin \
    -e KEYCLOAK_ADMIN_PASSWORD=admin \
    --env MY_ENV_VARIABLE='Value of my env variable' \
    -p 8080:8080 \
    docker-keycloak-with-theme

To be able to access MY_ENV_VARIABLE value in your theme. (This is assuming you're using docker but for for Helm for example see ).

To implement this you need to use like so:

vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { keycloakify } from "keycloakify/vite-plugin";

export default defineConfig({
  plugins: [
    react(), 
    keycloakify({
      extraThemeProperties: [ 
        "MY_ENV_VARIABLE=${env.MY_ENV_VARIABLE:default}"
      ]
    })
  ],
})
package.json
{
    "keycloakify": {
        "extraThemeProperties": [ 
            "MY_ENV_VARIABLE=${env.MY_ENV_VARIABLE:default}"
        ]
    }
}

Then you will be able to access the value of MY_ENV_VARIABLE with kcContext.properties.MY_ENV_VARIABLE. Example:

login/Template.tsx
import { useEffect } from "react";
import { type TemplateProps } from "keycloakify/login/TemplateProps";
import type { KcContext } from "./kcContext";
import type { I18n } from "./i18n";

export default function Template(props: TemplateProps<KcContext, I18n>) {
    const { kcContext } = props;

    useEffect(() => {
        console.log(kcContext.properties.MY_ENV_VARIABLE);
    }, []);

    //...

}

You also probably want to provide mock values the variables for when you're developing your theme in Storybook:

login/kcContext.ts
import { createGetKcContext } from "keycloakify/login";

export const { getKcContext } = createGetKcContext({
  mockData: [ /* ... */],
  mockProperties: {
    MY_ENV_VARIABLE: "Mocked value"
  }
});

export const { kcContext } = getKcContext({
  // Uncomment to test the login page for development.
  //mockPageId: "login.ftl",
});

export type KcContext = NonNullable<ReturnType<typeof getKcContext>["kcContext"]>;
🔧
this
the extraThemeProperties build option