keycloak-theme-react/README.md

224 lines
10 KiB
Markdown
Raw Normal View History

2022-09-06 17:22:23 +00:00
<p align="center">
2023-09-04 02:33:54 +00:00
<i>🚀 A starter/demo project for <a href="https://keycloakify.dev">Keycloakify</a> v9 🚀</i>
2023-03-17 14:05:28 +00:00
<br/>
<br/>
2023-02-26 16:42:16 +00:00
<img src="https://github.com/codegouvfr/keycloakify-starter/workflows/ci/badge.svg?branch=main">
2023-03-17 14:05:28 +00:00
<br/>
<br/>
<a href="https://starter.keycloakify.dev">Authenticated React SPA</a>
2022-09-06 17:22:23 +00:00
</p>
# Introduction
2024-01-10 12:38:55 +00:00
This repo constitutes an easily reusable setup for a Keycloak theme project OR for a SPA React App that generates a
Keycloak theme that goes along with it.
2023-12-11 17:24:03 +00:00
If you are only looking to create a Keycloak theme (and not a Keycloak theme and an App that share the same codebase) there are a lot of things that you can remove from this starter: [Please read this section of the README](#standalone-keycloak-theme).
2022-09-06 17:22:23 +00:00
2023-03-27 04:30:59 +00:00
> ❗️ WARNING ❗️: Don't waste time trying to port this setup to [Vite](https://vitejs.dev/).
2023-11-22 10:44:12 +00:00
> Currently Keycloakify only works collocated with Webpack projects but [we are working toward enabling collocation with Vite](https://github.com/keycloakify/keycloakify/pull/275)!
2023-03-21 22:40:16 +00:00
2022-09-06 17:22:23 +00:00
# Quick start
```bash
2023-04-20 00:22:43 +00:00
git clone https://github.com/keycloakify/keycloakify-starter
cd keycloakify-starter
2023-04-16 23:25:17 +00:00
yarn # install dependencies (it's like npm install)
yarn storybook # Start Storybook
# This is by far the best way to develop your theme
# This enable to quickly see your pages in isolation and in different states.
# You can create stories even for pages that you haven't explicitly overloaded. See src/keycloak-theme/login/pages/LoginResetPassword.stories.tsx
# See Keycloakify's storybook for if you need a starting point for your stories: https://github.com/keycloakify/keycloakify/tree/main/stories
2023-03-21 23:59:31 +00:00
yarn start # See the Hello World app
# Uncomment line 97 of src/keycloak-theme/login/kcContext where it reads: `mockPageId: "login.ftl"`, reload https://localhost:3000
2023-12-11 17:25:58 +00:00
# You can now see the login.ftl page with the mock data. (Don't forget to comment it back when you're done)
2023-12-06 05:38:17 +00:00
2023-12-06 19:27:48 +00:00
# Install mvn (Maven) if not already done. On mac it's 'brew install maven', on Ubuntu/Debian it's 'sudo apt-get install maven'
2023-12-11 17:26:54 +00:00
yarn build-keycloak-theme # Actually build the theme (generates the .jar to be imported in Keycloak)
# Read the instruction printed on the console to see how to test
# your theme on a real Keycloak instance.
2023-04-16 22:15:41 +00:00
npx eject-keycloak-page # Prompt that let you select the pages you want to customize
2023-04-18 15:36:52 +00:00
# This CLI tools is not guaranty to work, you can always copy pase pages
# from the Keycloakify repo.
2023-03-21 23:59:31 +00:00
npx initialize-email-theme # For initializing your email theme
# Note that Keycloakify does not feature React integration for email yet.
2023-03-21 23:59:31 +00:00
npx download-builtin-keycloak-theme # For downloading the default theme (as a reference)
# Look for the files in build_keycloak/src/main/resources/theme/{base,keycloak}
2022-09-06 17:22:23 +00:00
```
2024-01-10 11:59:42 +00:00
# Theme variant
Keycloakify enables you to create different variant for a single theme.
This enable you to have a single jar that embed two or more theme variant.
![Theme variant](https://content.gitbook.com/content/FcBKODbZbNDgm0rc6a9K/blobs/9iKgs2rv2Kfb2pbs4dRz/image.png)
You can enable this feature by providing multiple theme name in the Keycloakify build option.
[See documentation](https://docs.keycloakify.dev/build-options#themename)
2022-09-06 17:22:23 +00:00
# The CI workflow
2023-04-19 03:07:19 +00:00
- You need to manually allow GitHub Action to push on your repository. For this reason the initial setup will fail. You need to enabled permission and re-run failed job: [see video](https://user-images.githubusercontent.com/6702424/213480604-0aac0ea7-487f-491d-94ae-df245b2c7ee8.mov).
- This CI is configured to publish [the app](https://starter.keycloakify.dev) on [GitHub Pages](https://github.com/codegouvfr/keycloakify-starter/blob/3617a71deb1a6544c3584aa8d6d2241647abd48c/.github/workflows/ci.yaml#L51-L76) and on [DockerHub](https://github.com/codegouvfr/keycloakify-starter/blob/3617a71deb1a6544c3584aa8d6d2241647abd48c/.github/workflows/ci.yaml#L78-L123) (as a Ngnix based docker image). In practice you probably want one or the other but not both... or neither if you are just building a theme (and not a theme + an app).
2023-03-17 13:53:05 +00:00
If you want to enable the CI to publish on DockerHub on your behalf go to repository `Settings` tab, then `Secrets` you will need to add two new secrets:
`DOCKERHUB_TOKEN`, you Dockerhub authorization token.
`DOCKERHUB_USERNAME`, Your Dockerhub username.
2023-02-26 16:42:16 +00:00
We deploy the demo app at [starter.keycloakify.dev](https://starter.keycloakify.dev) using GitHub page on the branch `gh-pages` (you have to enable it).
2022-09-06 17:22:23 +00:00
To configure your own domain name please refer to [this documentation](https://docs.gitlanding.dev/using-a-custom-domain-name).
- To release **don't create a tag manually**, the CI do it for you. Just update the `package.json`'s version field and push.
- The `.jar` files that bundle the Keycloak theme will be attached as an asset with every GitHub release. [Example](https://github.com/InseeFrLab/keycloakify-starter/releases/tag/v0.1.0). The permalink to download the latest version is: `https://github.com/USER/PROJECT/releases/latest/download/keycloak-theme.jar`.
2023-02-26 16:42:16 +00:00
For this demo repo it's [here](https://github.com/codegouvfr/keycloakify-starter/releases/latest/download/keycloak-theme.jar)
2022-09-06 17:22:23 +00:00
- The CI publishes the app docker image on DockerHub. `<org>/<repo>:main` for each **commit** on `main`, `<org>/<repo>:<feature-branch-name>` for each **pull-request** on `main`
and when **releasing a new version**: `<org>/<repo>:latest` and `<org>/<repo>:X.Y.Z`
2023-02-26 16:42:16 +00:00
[See on DockerHub](https://hub.docker.com/r/codegouvfr/keycloakify-starter)
2022-09-06 17:22:23 +00:00
2023-04-01 14:54:32 +00:00
![image](https://user-images.githubusercontent.com/6702424/229296422-9d522707-114e-4282-93f7-01ca38c3a1e0.png)
2023-03-17 17:09:53 +00:00
2023-04-01 14:54:32 +00:00
![image](https://user-images.githubusercontent.com/6702424/229296556-a69f2dc9-4653-475c-9c89-d53cf33dc05a.png)
2022-09-06 17:22:23 +00:00
2023-04-17 00:58:17 +00:00
# The storybook
![image](https://user-images.githubusercontent.com/6702424/232350420-1921af90-d33e-492e-9296-0083298a84fa.png)
```bash
yarn
yarn storybook
```
# Docker
2022-09-06 17:22:23 +00:00
Instructions for building and running the react app (`src/App`) that is collocated with our Keycloak theme.
2022-09-06 17:22:23 +00:00
```bash
docker build -f Dockerfile -t keycloakify/keycloakify-starter:main .
docker run -it -dp 8083:80 keycloakify/keycloakify-starter:main
# You can access the app at http://localhost:8083
2022-09-06 17:22:23 +00:00
```
2024-01-10 12:38:55 +00:00
# I only want a Keycloak theme
2022-09-06 17:22:23 +00:00
2024-01-10 12:38:55 +00:00
If you are only looking to create a Keycloak theme and not a Theme + a React app, you can run theses few commands
2022-09-06 17:22:23 +00:00
after clicking ![image](https://user-images.githubusercontent.com/6702424/98155461-92395e80-1ed6-11eb-93b2-98c64453043f.png) to refactor the template
and remove unnecessary files.
2022-09-06 17:22:23 +00:00
```bash
rm -r src/App
2023-02-27 09:13:07 +00:00
mv src/keycloak-theme/* src/
rm -r src/keycloak-theme
2022-09-06 17:22:23 +00:00
cat << EOF > src/index.tsx
import { createRoot } from "react-dom/client";
2023-03-21 23:59:31 +00:00
import { StrictMode, lazy, Suspense } from "react";
import { kcContext as kcLoginThemeContext } from "./login/kcContext";
import { kcContext as kcAccountThemeContext } from "./account/kcContext";
2022-09-06 17:22:23 +00:00
2023-03-21 23:59:31 +00:00
const KcLoginThemeApp = lazy(() => import("./login/KcApp"));
const KcAccountThemeApp = lazy(() => import("./account/KcApp"));
2022-09-06 17:22:23 +00:00
createRoot(document.getElementById("root")!).render(
<StrictMode>
2023-03-21 23:59:31 +00:00
<Suspense>
{(()=>{
if( kcLoginThemeContext !== undefined ){
return <KcLoginThemeApp kcContext={kcLoginThemeContext} />;
}
if( kcAccountThemeContext !== undefined ){
return <KcAccountThemeApp kcContext={kcAccountThemeContext} />;
}
throw new Error(
"This app is a Keycloak theme" +
"It isn't meant to be deployed outside of Keycloak"
);
})()}
</Suspense>
2022-10-03 08:49:34 +00:00
</StrictMode>
2022-09-06 17:22:23 +00:00
);
2023-03-21 23:59:31 +00:00
2022-09-06 17:22:23 +00:00
EOF
2023-06-20 13:04:00 +00:00
rm .dockerignore Dockerfile nginx.conf
2022-09-06 17:22:23 +00:00
cat << EOF > .github/workflows/ci.yaml
name: ci
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
2023-12-01 22:25:33 +00:00
test:
2022-09-06 17:22:23 +00:00
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
2023-12-01 22:25:33 +00:00
- uses: actions/setup-node@v2
2022-09-06 17:22:23 +00:00
- uses: bahmutov/npm-install@v1
- run: yarn build
- run: npx keycloakify
2023-12-01 22:25:33 +00:00
env:
XDG_CACHE_HOME: "/home/runner/.cache/yarn"
2022-09-06 17:22:23 +00:00
check_if_version_upgraded:
name: Check if version upgrade
2023-12-01 22:25:33 +00:00
if: github.event_name == 'push'
2022-09-06 17:22:23 +00:00
runs-on: ubuntu-latest
2023-12-01 22:25:33 +00:00
needs: test
2022-09-06 17:22:23 +00:00
outputs:
2023-12-04 12:36:26 +00:00
from_version: \${{ steps.step1.outputs.from_version }}
to_version: \${{ steps.step1.outputs.to_version }}
2023-12-04 12:36:00 +00:00
is_upgraded_version: \${{ steps.step1.outputs.is_upgraded_version }}
2022-09-06 17:22:23 +00:00
steps:
2023-07-31 03:22:55 +00:00
- uses: garronej/ts-ci@v2.1.0
2022-09-06 17:22:23 +00:00
id: step1
with:
action_name: is_package_json_version_upgraded
2023-12-04 12:36:00 +00:00
branch: \${{ github.head_ref || github.ref }}
2022-09-06 17:22:23 +00:00
create_github_release:
runs-on: ubuntu-latest
2023-12-01 22:25:33 +00:00
needs: check_if_version_upgraded
2022-09-06 17:22:23 +00:00
# We create a release only if the version have been upgraded and we are on a default branch
2023-12-01 22:25:33 +00:00
if: needs.check_if_version_upgraded.outputs.is_upgraded_version == 'true' && github.event_name == 'push'
2022-09-06 17:22:23 +00:00
steps:
2023-12-01 22:25:33 +00:00
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: bahmutov/npm-install@v1
- run: yarn build
- run: npx keycloakify
env:
XDG_CACHE_HOME: "/home/runner/.cache/yarn"
- run: mv build_keycloak/target/retrocompat-*.jar retrocompat-keycloak-theme.jar
- run: mv build_keycloak/target/*.jar keycloak-theme.jar
2022-09-06 17:22:23 +00:00
- uses: softprops/action-gh-release@v1
with:
2023-12-04 12:36:00 +00:00
name: Release v\${{ needs.check_if_version_upgraded.outputs.to_version }}
tag_name: v\${{ needs.check_if_version_upgraded.outputs.to_version }}
target_commitish: \${{ github.head_ref || github.ref }}
2022-09-06 17:22:23 +00:00
generate_release_notes: true
draft: false
2023-12-01 22:25:33 +00:00
files: |
retrocompat-keycloak-theme.jar
keycloak-theme.jar
2022-09-06 17:22:23 +00:00
env:
2023-12-04 12:36:00 +00:00
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
2023-12-01 22:25:33 +00:00
2023-04-01 15:46:34 +00:00
EOF
2022-09-06 17:22:23 +00:00
```
2024-01-10 12:15:22 +00:00
You can also remove `oidc-spa`, `powerhooks`, `zod` and `tsafe` from your dependencies.