🔏 Customize key cloak's pages as if they were part of your App 🔏
Ultimately this build tool Generates a Keycloak theme
Motivations
The problem:
When we redirected to Keycloak the user suffers from a harsh context switch. Keycloak does offer a way to customize theses pages but it requires a lot of raw HTML/CSS hacking to reproduce the look and feel of a specific app. Not mentioning the maintenance cost of such an endeavour.
Wouldn't it be great if we could just design the login and register pages as if they where part of our app?
Here is yarn add keycloakify
for you 🍸
TODO: Insert video after.
How to use
Setting up the build tool
Add keycloakify
to the dev dependencies of your project npm install --save-dev keycloakify
or yarn add --dev keycloakify
then configure your package.json
build's script to build the keycloak's theme by adding && build-keycloak-theme
.
Typically you will get:
package.json
"devDependencies": {
"keycloakify": "^0.0.10"
},
"scripts": {
"build": "react-scripts build && build-keycloak-theme"
},
Then build your app with yarn run build
or npm run build
, you will be provided with instructions
about how to load the theme into Keycloak.
Developing your login and register pages in your React app
Just changing the look
The fist approach is to only arr/replace the default class names by your own.
import { App } from "./<wherever>/App";
import {
KcApp,
defaultKcProps,
kcContext
} from "keycloakify";
import { css } from "tss-react";
const myClassName = css({ "color": "red" });
reactDom.render(
// Unless the app is currently being served by Keycloak
// kcContext is undefined.
kcContext !== undefined ?
<KcApp
kcContext={kcContext}
{...{
...defaultKcProps,
"kcHeaderWrapperClass": myClassName
}}
/> :
<App />, // Your actual app
document.getElementById("root")
);
result:
Changing the look and feel
If you want to really re-implement the pages the best approach is to
create you own version of the <KcApp />
.
Copy/past some of the components provided by this module and start hacking around.
Hot reload
By default, in order to see your changes you will have to wait for
yarn build
to complete which can takes sevrall minute.
If you want to test your login screens outside of Keycloak, in storybook
for example you can use kcContextMocks
.
import {
KcApp,
defaultKcProps,
kcContextMocks
} from "keycloakify";
reactDom.render(
kcContext !== undefined ?
<KcApp
kcContext={kcContextMocks.kcLoginContext}
{...defaultKcProps}
/>
document.getElementById("root")
);
then yarn start
...
NOTE: keycloak-react-theming was renamed keycloakify since this video was recorded
How to implement context persistance
If you want dark mode preference, language and others users preferences to persist within the page served by keycloak here are the methods you can adopt.
If your keycloak is a subdomain of your app.
E.g: Your app url is my-app.com
and your keycloak url is auth.my-app.com
.
In this case there is a very straightforward approach and it is to use powerhooks/useGlobalState
.
Instead of { "persistance": "localStorage" }
use { "persistance": "cookie" }
.
Else
You will have to use URL parameters to passes states when you redirect to the login page.
TOTO: Provide a clean way, as abstracted as possible, way to do that.
REQUIREMENTS
This tools assumes you are bundling your app with Webpack (tested with 4.44.2) .
It assumes there is a build/
directory at the root of your react project directory containing a index.html
file
and a static/
directory generated by webpack.
All this is defaults with create-react-app
(tested with 4.0.3=)
- For building the theme:
mvn
(Maven) must be installed - For development, (testing the theme in a local container ):
rm
,mkdir
,wget
,unzip
are assumed to be available anddocker
up and running.
NOTE: This build tool has only be tested on MacOS.
API Reference
The build tool
Part of the lib that runs with node, at build time.
npx build-keycloak-theme
: Builds the theme, the CWD is assumed to be the root of your react project.npx download-sample-keycloak-themes
: Downloads the keycloak default themes (for development purposes)
The fronted lib ( imported into your react app )
Part of the lib that you import in your react project and runs on the browser.
TODO