From a4276bfd68b6fa226b8a1175cbda785a0d23362b Mon Sep 17 00:00:00 2001 From: garronej Date: Thu, 20 Apr 2023 04:41:29 +0200 Subject: [PATCH] Update Keycloakify and setup referrerURI for account --- package.json | 4 ++-- src/App/App.tsx | 29 +++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index b42fba8..7f61826 100755 --- a/package.json +++ b/package.json @@ -37,8 +37,8 @@ "evt": "^2.4.15", "jwt-decode": "^3.1.2", "keycloak-js": "^21.0.1", - "keycloakify": "7.9.0-rc.0", - "powerhooks": "^0.26.7", + "keycloakify": "^7.9.2", + "powerhooks": "^0.26.8", "react": "18.1.0", "react-dom": "18.1.0", "tsafe": "^1.6.0" diff --git a/src/App/App.tsx b/src/App/App.tsx index 4f3cccb..fd1bc9a 100644 --- a/src/App/App.tsx +++ b/src/App/App.tsx @@ -4,16 +4,18 @@ import myimg from "./myimg.png"; import { createOidcClientProvider, useOidcClient } from "./oidc"; import { addFooToQueryParams, addBarToQueryParams } from "../keycloak-theme/login/valuesTransferredOverUrl"; import jwt_decode from "jwt-decode"; +import { addParamToUrl } from "powerhooks/tools/urlSearchParams"; //On older Keycloak version you need the /auth (e.g: http://localhost:8080/auth) //On newer version you must remove it (e.g: http://localhost:8080 ) const keycloakUrl = "https://auth.code.gouv.fr/auth"; const keycloakRealm = "keycloakify"; +const keycloakClient= "starter"; const { OidcClientProvider } = createOidcClientProvider({ url: keycloakUrl, realm: keycloakRealm, - clientId: "starter", + clientId: keycloakClient, //This function will be called just before redirecting, //it should return the current langue. //kcContext.locale.currentLanguageTag will be what this function returned just before redirecting. @@ -40,6 +42,29 @@ function ContextualizedApp() { const { oidcClient } = useOidcClient(); + let accountUrl = `${keycloakUrl}/realms/${keycloakRealm}/account`; + + // Set the language the user will get on the account page + accountUrl = addParamToUrl({ + "url": accountUrl, + "name": "kc_locale", + "value": "en" + }).newUrl; + + // Enable to redirect to the app from the account page we'll get the referrer_uri under kcContext.url.referrerUri + // see: https://technology.first8.nl/add-manage-account-link-keycloak-redhat-sso/ + accountUrl = addParamToUrl({ + "url": accountUrl, + "name": "referrer", + "value": keycloakClient + }).newUrl; + + accountUrl = addParamToUrl({ + "url": accountUrl, + "name": "referrer_uri", + "value": window.location.href + }).newUrl; + return (
@@ -48,7 +73,7 @@ function ContextualizedApp() { <>

You are authenticated !

{/* On older Keycloak version its /auth/realms instead of /realms */} - Link to your Keycloak account + Link to your Keycloak account
{JSON.stringify(jwt_decode(oidcClient.getAccessToken()), null, 2)}