In your Webpack Project
Last updated
Last updated
If you have a Webpack/React/TypeScript project you can integrate Keycloakify directly inside it.
In this guide we're going to work with a vanilla Create React App project.
Before anything make sure to commit all your pending changes so you can easily revert changes if need be.
Let's start by installing Keycloakify (and optionally Storybook) to our project:
Next we want to repatriate the relevant files from the starter template into our project:
Now you want to modify your entry point so that:
If the kcContext global is defined, render your Keycloakify theme
Else, render your App as usual.
Question:
Why do my main application and Keycloak theme share the same entry point?
Answer:
To simplify the build process. If you don't want it to negatively impact the performance of your application, it's essential to understand the following points:
Different Contexts: The application (App
) and Keycloak page (KcPage
) are mounted in very different contexts. Avoid sharing providers between the two at the main.tsx
file level. The true entry point of your application is the App
component, while the entry point for your Keycloak theme is the KcPage
component. Be careful about what code is shared between them.
Responsibility of main.tsx: The main.tsx
file should only determine the context (either the application or Keycloak) and mount the appropriate component (App
or KcPage
). It should not contain any substantial logic or dependencies.
Performance Considerations: Keep main.tsx
as lightweight as possible to avoid increasing the initial load time of both your main application and login pages. For example, do not load any state management libraries like redux-toolkit
at this level.
Finally you want to add some script for Keycloakify in you package.json and also let Keycloakify know about how your Webpack project is configured.
Leave accountThemeImplementation set to "none" for now. To initialize the account theme refer to this guide.
Keycloakify has many build options that you can use, however projectBuildDirPath
, staticDirPathInProjectBuildDirPath
and publicDirPath
are parameters specific to the use of Keycloakify in a Webpack context.
Theses are not preferences! If you're not using Create React App your Webpack configuration is probably different and you want to update those values to reflect how webpack build your site in your project.
That's it, your project is ready to go!
You can run npm run build-keycloak-theme
, the JAR distribution of your Keycloak theme will be generated in build_keycloak
(you can change this).
You're now able to use all the Keycloakify commands (npx keycloakify --help
) from the root of your project.
If you're currently using keycloak-js or react-oidc-context to manage user authentication in your app you might want to checkout oidc-spa, the alternative from the Keycloakify team.
If you have any issues reach out on Discord! We're here to help!