Skip to content

Commit

Permalink
👌 [#390] Make context provider specific to OIDC
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Oct 10, 2024
1 parent 1b1b26e commit 89fd3ca
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 35 deletions.
12 changes: 6 additions & 6 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import { useAsync } from "react-use";

import "./App.css";
import { User, getOIDCInfo, whoAmI } from "./lib/api/auth";
import ExtraConfigContext, {
OidcInfo,
} from "./lib/contexts/ExtraConfigContext";
import OidcConfigContext, {
OidcConfigContextType,
} from "./lib/contexts/OidcConfigContext";
import { formatUser } from "./lib/format/user";

function App() {
Expand All @@ -41,7 +41,7 @@ function App() {
const handle = match?.handle as Record<string, unknown>;

const [user, setUser] = useState<User | null>(null);
const [oidcInfo, setOidcInfo] = useState<OidcInfo>({
const [oidcInfo, setOidcInfo] = useState<OidcConfigContextType>({
enabled: false,
loginUrl: "",
});
Expand Down Expand Up @@ -162,11 +162,11 @@ function App() {
],
}}
>
<ExtraConfigContext.Provider value={{ oidc: oidcInfo }}>
<OidcConfigContext.Provider value={oidcInfo}>
<ModalService>
<Outlet />
</ModalService>
</ExtraConfigContext.Provider>
</OidcConfigContext.Provider>
</NavigationContext.Provider>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cacheDelete, cacheMemo } from "../cache/cache";
import { OidcInfo } from "../contexts/ExtraConfigContext";
import { OidcConfigContextType } from "../contexts/OidcConfigContext";
import { request } from "./request";

export type User = {
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function whoAmI() {
export async function getOIDCInfo() {
return cacheMemo("getOIDCInfo", async () => {
const response = await request("GET", "/oidc-info");
const promise: Promise<OidcInfo> = response.json();
const promise: Promise<OidcConfigContextType> = response.json();
return promise;
});
}
16 changes: 0 additions & 16 deletions frontend/src/lib/contexts/ExtraConfigContext.ts

This file was deleted.

13 changes: 13 additions & 0 deletions frontend/src/lib/contexts/OidcConfigContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createContext } from "react";

export type OidcConfigContextType = {
enabled: boolean;
loginUrl: string;
};

const OidcConfigContext: React.Context<OidcConfigContextType> = createContext({
enabled: false,
loginUrl: "",
} as OidcConfigContextType);

export default OidcConfigContext;
12 changes: 5 additions & 7 deletions frontend/src/pages/login/Login.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Meta, StoryObj } from "@storybook/react";
import { expect, within } from "@storybook/test";

import { ReactRouterDecorator } from "../../../.storybook/decorators";
import ExtraConfigContext from "../../lib/contexts/ExtraConfigContext";
import OidcConfigContext from "../../lib/contexts/OidcConfigContext";
import { LoginPage } from "./Login";

const meta: Meta<typeof LoginPage> = {
Expand All @@ -23,16 +23,14 @@ export const LoginPageStory: Story = {

export const LoginPageWithOIDC: Story = {
render: (args) => (
<ExtraConfigContext.Provider
<OidcConfigContext.Provider
value={{
oidc: {
enabled: true,
loginUrl: "http://backend.nl/oidc/authenticate",
},
enabled: true,
loginUrl: "http://backend.nl/oidc/authenticate",
}}
>
<LoginPage />
</ExtraConfigContext.Provider>
</OidcConfigContext.Provider>
),
play: async (context) => {
const canvas = within(context.canvasElement);
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/pages/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { useContext } from "react";
import { useActionData, useSubmit } from "react-router-dom";

import ExtraConfigContext from "../../lib/contexts/ExtraConfigContext";
import OidcConfigContext from "../../lib/contexts/OidcConfigContext";
import "./Login.css";

export type LoginProps = React.ComponentProps<"main"> & {
Expand All @@ -30,7 +30,8 @@ const makeRedirectUrl = (oidcLoginUrl: string) => {
* Login page
*/
export function LoginPage({ ...props }: LoginProps) {
const { oidc } = useContext(ExtraConfigContext);
const { enabled: oidcEnabled, loginUrl: oidcLoginUrl } =
useContext(OidcConfigContext);

const fields = [
{
Expand Down Expand Up @@ -60,8 +61,8 @@ export function LoginPage({ ...props }: LoginProps) {
const { detail, nonFieldErrors, ...errors } = formErrors;

const oidcProps: Partial<LoginTemplateProps> = {};
if (oidc?.enabled) {
oidcProps.urlOidcLogin = makeRedirectUrl(oidc.loginUrl);
if (oidcEnabled) {
oidcProps.urlOidcLogin = makeRedirectUrl(oidcLoginUrl);
oidcProps.labelOidcLogin = "Organisatie login";
}

Expand Down

0 comments on commit 89fd3ca

Please sign in to comment.