Skip to content

Commit ea0ae0f

Browse files
committed
in account page, redirect to login theme pages
1 parent 9f6aeaa commit ea0ae0f

File tree

7 files changed

+87
-42
lines changed

7 files changed

+87
-42
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"@codegouvfr/react-dsfr": "^1.14.10",
2424
"keycloakify": "^11.3.32",
25+
"oidc-spa": "^5.6.1",
2526
"react": "^18.2.0",
2627
"react-dom": "^18.2.0"
2728
},

src/account/KcPage.tsx

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,40 @@ const DefaultPage = lazy(() => import("keycloakify/account/DefaultPage"));
66
const DefaultTemplate = lazy(() => import("keycloakify/account/Template"));
77
const Template = lazy(() => import("./Template"));
88
const Account = lazy(() => import("./pages/Account"));
9+
import { OidcProvider } from "./oidc";
910

1011
export default function KcPage(props: { kcContext: KcContext }) {
1112
const { kcContext } = props;
1213

1314
const { i18n } = useI18n({ kcContext });
1415

1516
return (
16-
<Suspense>
17-
{(() => {
18-
switch (kcContext.pageId) {
19-
case "account.ftl":
20-
return (
21-
<Account
22-
{...{ kcContext, i18n, classes }}
23-
Template={Template}
24-
doUseDefaultCss={false}
25-
/>
26-
);
27-
default:
28-
return (
29-
<DefaultPage
30-
kcContext={kcContext}
31-
i18n={i18n}
32-
classes={classes}
33-
Template={DefaultTemplate}
34-
doUseDefaultCss={true}
35-
/>
36-
);
37-
}
38-
})()}
39-
</Suspense>
17+
<OidcProvider>
18+
<Suspense>
19+
{(() => {
20+
switch (kcContext.pageId) {
21+
case "account.ftl":
22+
return (
23+
<Account
24+
{...{ kcContext, i18n, classes }}
25+
Template={Template}
26+
doUseDefaultCss={false}
27+
/>
28+
);
29+
default:
30+
return (
31+
<DefaultPage
32+
kcContext={kcContext}
33+
i18n={i18n}
34+
classes={classes}
35+
Template={DefaultTemplate}
36+
doUseDefaultCss={true}
37+
/>
38+
);
39+
}
40+
})()}
41+
</Suspense>
42+
</OidcProvider>
4043
);
4144
}
4245

src/account/Template.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
100100
burgerMenuButtonText={"Menu"}
101101
items={[
102102
{ text: msg("account"), linkProps: { href: url.accountUrl }, isActive: active === "account" },
103-
...(features.passwordUpdateSupported
104-
? [{ text: msg("password"), linkProps: { href: url.passwordUrl }, isActive: active === "password" }]
105-
: []),
103+
// ...(features.passwordUpdateSupported
104+
// ? [{ text: msg("password"), linkProps: { href: url.passwordUrl }, isActive: active === "password" }]
105+
// : []),
106106
{ text: msg("authenticator"), linkProps: { href: url.totpUrl }, isActive: active === "totp" },
107107
...(features.identityFederation
108108
? [{ text: msg("federatedIdentity"), linkProps: { href: url.socialUrl }, isActive: active === "social" }]

src/account/i18n.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { i18nBuilder } from "keycloakify/account";
22
import type { ThemeName } from "../kc.gen";
33

4+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
45
const { useI18n, ofTypeI18n } = i18nBuilder
56
.withThemeName<ThemeName>()
67
.withExtraLanguages({})
7-
.withCustomTranslations({})
8+
.withCustomTranslations({
9+
en: { updateProfile: "Update profile", deleteAccount: "Delete account" },
10+
fr: {
11+
updateProfile: "Mettre à jour le profil",
12+
deleteAccount: "Supprimer le compte"
13+
}
14+
})
815
.build();
916

1017
type I18n = typeof ofTypeI18n;

src/account/oidc.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createReactOidc } from "oidc-spa/react";
2+
import { createMockReactOidc } from "oidc-spa/mock/react";
3+
4+
const publicUrl = undefined;
5+
const isAuthGloballyRequired = true;
6+
7+
export const realm = import.meta.env.DEV ? "" : window.location.pathname.split("/")[2];
8+
9+
export const { OidcProvider, useOidc, getOidc } = import.meta.env.DEV
10+
? createMockReactOidc({
11+
isUserInitiallyLoggedIn: true,
12+
publicUrl,
13+
isAuthGloballyRequired
14+
})
15+
: createReactOidc({
16+
issuerUri: `${window.location.origin}/realms/${realm}`,
17+
clientId: "account-console",
18+
publicUrl,
19+
isAuthGloballyRequired
20+
});

src/account/pages/Account.tsx

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { PageProps } from "keycloakify/account/pages/PageProps";
44
import { clsx } from "keycloakify/tools/clsx";
55
import type { KcContext } from "../KcContext";
66
import type { I18n } from "../i18n";
7+
import { useOidc } from "../oidc";
78

89
export default function Account(props: PageProps<Extract<KcContext, { pageId: "account.ftl" }>, I18n>) {
910
const { kcContext, i18n, doUseDefaultCss, Template } = props;
@@ -13,38 +14,46 @@ export default function Account(props: PageProps<Extract<KcContext, { pageId: "a
1314
kcBodyClass: clsx(props.classes?.kcBodyClass, "user")
1415
};
1516

17+
const { goToAuthServer } = useOidc();
1618
const { referrer } = kcContext;
1719

1820
const { msg } = i18n;
1921

2022
return (
2123
<Template {...{ kcContext, i18n, doUseDefaultCss, classes }} active="account">
2224
<h2 className={fr.cx("fr-h2")}>{msg("editAccountHtmlTitle")}</h2>
23-
<ButtonsGroup
25+
<ButtonsGroup
2426
buttons={[
2527
{
26-
children: "Update profil",
27-
linkProps: {
28-
href: `/realms/myrealm/login-actions/required-action?execution=UPDATE_PROFILE&client_id=${referrer?.name}`,
29-
},
28+
children: msg("updateProfile"),
29+
onClick: () =>
30+
goToAuthServer({
31+
extraQueryParams: { kc_action: "UPDATE_PROFILE" }
32+
})
3033
},
3134
{
32-
children: "Update password",
33-
linkProps: {
34-
href: `/realms/myrealm/login-actions/required-action?execution=UPDATE_PASSWORD&client_id=${referrer?.name}`,
35-
},
35+
children: msg("updatePasswordTitle"),
36+
onClick: () =>
37+
goToAuthServer({
38+
extraQueryParams: { kc_action: "UPDATE_PASSWORD" }
39+
})
3640
},
3741
{
38-
children: "Delete account",
39-
linkProps: {
40-
href: `/realms/myrealm/login-actions/required-action?execution=delete_account&client_id=${referrer?.name}`,
41-
},
42+
children: msg("deleteAccount"),
43+
onClick: () =>
44+
goToAuthServer({
45+
extraQueryParams: { kc_action: "delete_account" }
46+
}),
4247
priority: "secondary"
4348
}
4449
]}
4550
/>
4651

47-
{referrer && <a className={fr.cx("fr-link")} href={referrer?.url} >{msg("backTo", referrer.name)}</a>}
52+
{referrer && (
53+
<a className={fr.cx("fr-link")} href={referrer?.url}>
54+
{msg("backTo", referrer.name)}
55+
</a>
56+
)}
4857
</Template>
4958
);
5059
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,6 +4875,11 @@ ohash@^1.1.3:
48754875
resolved "https://registry.yarnpkg.com/ohash/-/ohash-1.1.3.tgz#f12c3c50bfe7271ce3fd1097d42568122ccdcf07"
48764876
integrity sha512-zuHHiGTYTA1sYJ/wZN+t5HKZaH23i4yI1HMwbuXm24Nid7Dv0KcuRlKoNKS9UNfAVSBlnGLcuQrnOKWOZoEGaw==
48774877

4878+
oidc-spa@^5.6.1:
4879+
version "5.6.1"
4880+
resolved "https://registry.yarnpkg.com/oidc-spa/-/oidc-spa-5.6.1.tgz#bd4953bad6671d10088f86540cbc8e2f6c85701f"
4881+
integrity sha512-ZC9AA+rnsy9xX76znf3qppbtUliedl1B2WLWSkD3xx2svOWdPlm7sy7yHzbU8pBsBas0HEcccJrJpeBD6MGdKw==
4882+
48784883
48794884
version "2.4.1"
48804885
resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.4.1.tgz#58c8c44116e54845ad57f14ab10b03533184ac3f"

0 commit comments

Comments
 (0)