Keycloakify
HomeGitHubStorybookAlternative to keycloak-js
v11
  • Documentation
  • Release Notes & Upgrade Instructions
  • FAQ
v11
  • Quick Start
  • CSS Customization
  • Testing your Theme
    • Outside of Keycloak
    • Inside of Keycloak
  • Deploying Your Theme
  • Integrating Keycloakify in your Codebase
    • Vite
    • Create-React-App / Webpack
    • yarn/npm/pnpm/bun Workspaces
    • Turborepo
    • Nx
    • Angular Workspace
  • Common Use Case Examples
    • Using a Component Library
    • Custom Fonts
    • Changing the background image
    • Adding your Logo
    • Using Tailwind
    • Dark Mode Persistence
  • Features
    • Internationalization and Translations
      • Basic principles
      • Previewing Your Pages in Different Languages
      • Adding New Translation Messages or Changing the Default Ones
      • Adding Support for Extra Languages
    • Theme Variants
    • Environment Variables
    • Styling a Custom Page Not Included in Base Keycloak
    • Integrating an Existing Theme into Your Keycloakify Project
    • Compiler Options
      • --project
      • keycloakVersionTargets
      • environmentVariables
      • themeName
      • startKeycloakOptions
      • themeVersion
      • accountThemeImplementation
      • postBuild
      • XDG_CACHE_HOME
      • kcContextExclusionsFtl
      • keycloakifyBuildDirPath
      • groupId
      • artifactId
      • Webpack specific options
        • projectBuildDirPath
        • staticDirPathInProjectBuildDirPath
        • publicDirPath
  • Page-Specific Guides
    • Registration Page
    • Terms and Conditions Page
  • Theme types
    • Differences Between Login Themes and Other Types of Themes
    • Account Theme
      • Single-Page
      • Multi-Page
    • Email Theme
    • Admin Theme
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Common Use Case Examples

Adding your Logo

Practical example of how to import custom assets in ejected components.

Last updated 4 months ago

Was this helpful?

NOTE: You can very well change the logo using only CSS without having to ejecting the template. There's a demo in .

Let's say you want to put the logo of your company on every pages of the theme.

First you'd eject the Template:

npx keycloakify eject-page # Select login -> Template.tsx

This will create a src/login/Template.tsx file in your project.

Now we can use the asset in our component:

src/login/Template.tsx
import logoPngUrl from "./assets/logo.png";
// ...
<div className={kcClsx("kcLoginClass")}>
    <div id="kc-header" className={kcClsx("kcHeaderClass")}>
        <div id="kc-header-wrapper" className={kcClsx("kcHeaderWrapperClass")}>
            {/*{msg("loginTitleHtml", realm.displayNameHtml)}*/}
            <img src={logoPngUrl} width={500}/>
        </div>
    </div>
    {/* ... */}
src/login/Template.svelte
<script lang="ts">
  import logoPngUrl from "./assets/logo.png";
  // ...
</script>

<div
  id="kc-header-wrapper"
  class={kcClsx('kcHeaderWrapperClass')}
>
  <!--{ msgStr('loginTitleHtml', realm.displayNameHtml) }-->
  <img src={logoPngUrl} width={500} />
</div>
src/login/template/template.component.ts
import logoPngUrl from '../assets/logo.png';

export class TemplateComponent extends ComponentReference {
  logoPngUrl = logoPngUrl;
  // ...
src/login/template/template.component.html
<img
  [src]="logoPngUrl"
  alt="logo"
  width="500"
/>

Result:

Optional: Updating the logo without re-building the theme

Some pepoles want to be able to "hot swipe" the asset in the Keycloak file system without having to re-build the theme and re-deploy it.

To ensure that the assets are located in a predictible location you would use the public/ directory.

Move the file to public/img/logo.png.

Then make an absolute import from your component:

It's important that you do not simply harcode src="/img/logo.png", or keycloakify won't be able to patch the URL for Keycloak. Use Vite's builtin import.meta.env.BASE_URL.

src/login/Template.tsx
<div className={kcClsx("kcLoginClass")}>
    <div id="kc-header" className={kcClsx("kcHeaderClass")}>
        <div id="kc-header-wrapper" className={kcClsx("kcHeaderWrapperClass")}>
            {/*{msg("loginTitleHtml", realm.displayNameHtml)}*/}
            <img src={`${import.meta.env.BASE_URL}img/logo.png`} width={500}/>
        </div>
    </div>
    {/* ... */}

Doing this is a good practice in any Vite project (not specially Keycloakify) since it ensure the correctness of your URLs even if you customize the base parameter in the your vite.config.ts. Hard coding "/img/logo.png" only works when base is "/" (which is the default)

It's important that you do not simply harcode src="/img/logo.png", or keycloakify won't be able to patch the URL for Keycloak. Use Vite's builtin import.meta.env.BASE_URL.

src/login/Template.svelte
<div
  id="kc-header-wrapper"
  class={kcClsx('kcHeaderWrapperClass')}
>
  <!--{ msgStr('loginTitleHtml', realm.displayNameHtml) }-->
  <img src={`${import.meta.env.BASE_URL}img/logo.png`} width={500} />
</div>

Doing this is a good practice in any Vite project (not specially Keycloakify) since it ensure the correctness of your URLs even if you customize the "base" parameter in the your vite.config.ts. Writing "/img/logo.png" only works when base is "/" (which is the default)

src/login/template/template.component.ts
export class TemplateComponent extends ComponentReference {
  BASE_URL = import.meta.env.BASE_URL;
src/login/template/template.component.html
<img
  [src]="BASE_URL + 'img/logo.png'"
  alt="logo"
  width="500"
/>

It's important that you do not simply harcode src="/img/logo.png", or keycloakify won't be able to patch the URL for Keycloak. Use PUBLIC_URL instead.

src/login/Template.tsx
import { PUBLIC_URL } from "keycloakify/PUBLIC_URL";

<div className={kcClsx("kcLoginClass")}>
    <div id="kc-header" className={kcClsx("kcHeaderClass")}>
        <div id="kc-header-wrapper" className={kcClsx("kcHeaderWrapperClass")}>
            {/*{msg("loginTitleHtml", realm.displayNameHtml)}*/}
            <img src={`${PUBLIC_URL}/img/logo.png`} width={500}/>
        </div>
    </div>
    {/* ... */}

NOTE: You can see PUBLIC_URL as an equivalent of process.env.PUBLIC_URL that will work inside and outside of Keycloak.

If you ever need to SSH into the Keycloak server and hot swipe the image you can find it at

Let's use this placeholder for the demo: and save it in src/login/assets/logo.png.

/opt/keycloak/themes//login/resources/dist/img/logo.png

logo.png
<name of your theme>
this video
Inspecting the Docker Keycloak docker image file system we can find the logo.png at the expected location.