Skip to content

Commit 8124a44

Browse files
move oidc config to frontend-config
1 parent a909ae1 commit 8124a44

File tree

5 files changed

+50
-40
lines changed

5 files changed

+50
-40
lines changed

frontend-config.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"backendUrl": "http://localhost:3000",
33
"landscape": "LOCAL",
4-
"documentationBaseUrl": "http://localhost:3000"
4+
"documentationBaseUrl": "http://localhost:3000",
5+
"oidcConfig": {
6+
"clientId": "clientId",
7+
"issuerUrl": "issuer-url",
8+
"scopes": [
9+
""
10+
]
11+
}
512
}
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
import { ReactNode, use } from 'react';
2-
import { AuthProvider, AuthProviderProps } from 'react-oidc-context';
1+
import { ReactNode } from 'react';
2+
import { AuthProvider } from 'react-oidc-context';
33
import { useFrontendConfig } from './FrontendConfigContext.tsx';
4-
import { LoadCrateKubeConfig } from '../lib/oidc/crate.ts';
4+
import { buildAuthProviderProps } from '../lib/oidc/onboardingApi.ts';
55

66
interface AuthProviderOnboardingProps {
77
children?: ReactNode;
88
}
99

10-
// Promise needs to be cached
11-
// https://react.dev/blog/2024/12/05/react-19#use-does-not-support-promises-created-in-render
12-
const fetchAuthPromiseCache = new Map<string, Promise<AuthProviderProps>>();
13-
1410
export function AuthProviderOnboarding({
1511
children,
1612
}: AuthProviderOnboardingProps) {
17-
const { backendUrl } = useFrontendConfig();
18-
19-
const fetchAuthConfigPromise =
20-
fetchAuthPromiseCache.get(backendUrl) ?? LoadCrateKubeConfig(backendUrl);
21-
fetchAuthPromiseCache.set(backendUrl, fetchAuthConfigPromise);
22-
23-
const authConfig = use(fetchAuthConfigPromise);
13+
const { oidcConfig } = useFrontendConfig();
2414

15+
const authConfig = buildAuthProviderProps(oidcConfig);
2516
return <AuthProvider {...authConfig}>{children}</AuthProvider>;
2617
}

src/context/FrontendConfigContext.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReactNode, createContext, use } from 'react';
22
import { DocLinkCreator } from '../lib/shared/links';
3+
import { OIDCConfig } from '../lib/oidc/onboardingApi';
34

45
export enum Landscape {
56
Live = 'LIVE',
@@ -9,10 +10,14 @@ export enum Landscape {
910
Local = 'LOCAL',
1011
}
1112

12-
interface FrontendConfigContextProps {
13+
type FrontendConfig = {
1314
backendUrl: string;
1415
landscape?: Landscape;
1516
documentationBaseUrl: string;
17+
oidcConfig: OIDCConfig;
18+
}
19+
20+
interface FrontendConfigContextProps extends FrontendConfig {
1621
links: DocLinkCreator;
1722
}
1823

@@ -21,7 +26,7 @@ export const FrontendConfigContext = createContext<FrontendConfigContextProps |
2126
);
2227

2328

24-
const fetchPromise = fetch('/frontend-config.json').then((res) => res.json());
29+
const fetchPromise = fetch('/frontend-config.json').then((res) => res.json()).then((data) => data as FrontendConfig);
2530

2631
interface FrontendConfigProviderProps {
2732
children: ReactNode;
@@ -32,11 +37,8 @@ export function FrontendConfigProvider({ children }: FrontendConfigProviderProps
3237
const docLinks = new DocLinkCreator(config.documentationBaseUrl);
3338
const value: FrontendConfigContextProps = {
3439
links: docLinks,
35-
backendUrl: config.backendUrl,
36-
landscape: config.landscape,
37-
documentationBaseUrl: config.documentationBaseUrl,
40+
...config,
3841
};
39-
4042
return (
4143
<FrontendConfigContext value={value}>{children}</FrontendConfigContext>
4244
);

src/lib/oidc/crate.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/lib/oidc/onboardingApi.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { WebStorageStateStore } from "oidc-client-ts";
2+
import { AuthProviderProps } from "react-oidc-context";
3+
4+
export interface OIDCConfig {
5+
clientId: string;
6+
issuerUrl: string;
7+
scopes: string[];
8+
}
9+
10+
export function buildAuthProviderProps(oidcConfig: OIDCConfig) {
11+
const userStore = new WebStorageStateStore({ store: window.localStorage });
12+
13+
const props: AuthProviderProps = {
14+
authority: oidcConfig.issuerUrl,
15+
client_id: oidcConfig.clientId,
16+
redirect_uri: getDefaultRedirectUri(),
17+
scope: oidcConfig.scopes.join(' '),
18+
userStore: userStore,
19+
automaticSilentRenew: false, // we show a window instead that asks the user to renew the token
20+
onSigninCallback: () => {
21+
window.history.replaceState({}, document.title, window.location.pathname);
22+
},
23+
};
24+
return props;
25+
}
26+
27+
function getDefaultRedirectUri() {
28+
return window.location.origin;
29+
}

0 commit comments

Comments
 (0)