Skip to content

Commit

Permalink
Show forbidden section only after whoAmI is set (keycloak#34589)
Browse files Browse the repository at this point in the history
closes keycloak#34402

Signed-off-by: Christian Janker <[email protected]>
(cherry picked from commit 6482e41)
Signed-off-by: Erik Jan de Wit <[email protected]>
  • Loading branch information
yanxch authored and edewit committed Dec 2, 2024
1 parent 3400602 commit c8f097b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions js/apps/admin-ui/src/context/whoami/WhoAmI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export class WhoAmI {
public isTemporary(): boolean {
return this.#me?.temporary ?? false;
}

public isEmpty(): boolean {
return !this.#me;
}
}

type WhoAmIProps = {
Expand Down
17 changes: 12 additions & 5 deletions js/apps/admin-ui/src/root/AuthWall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useMatches } from "react-router-dom";

import { ForbiddenSection } from "../ForbiddenSection";
import { useAccess } from "../context/access/Access";
import { useWhoAmI } from "../context/whoami/WhoAmI";
import { KeycloakSpinner } from "@keycloak/keycloak-ui-shared";

function hasProp<K extends PropertyKey>(
data: object,
Expand All @@ -14,6 +16,7 @@ function hasProp<K extends PropertyKey>(
export const AuthWall = ({ children }: any) => {
const matches = useMatches();
const { hasAccess } = useAccess();
const { whoAmI } = useWhoAmI();

const permissionNeeded = matches.flatMap(({ handle }) => {
if (
Expand All @@ -31,9 +34,13 @@ export const AuthWall = ({ children }: any) => {
return [handle.access] as AccessType[];
});

return hasAccess(...permissionNeeded) ? (
children
) : (
<ForbiddenSection permissionNeeded={permissionNeeded} />
);
if (whoAmI.isEmpty()) {
return <KeycloakSpinner />;
}

if (!hasAccess(...permissionNeeded)) {
return <ForbiddenSection permissionNeeded={permissionNeeded} />;
}

return children;
};

0 comments on commit c8f097b

Please sign in to comment.