Skip to content

Commit 4cf061c

Browse files
committed
ui: Display the authenticated userId by configured userIdClaim
Signed-off-by: Marco Franssen <marco.franssen@gmail.com>
1 parent e03ee3a commit 4cf061c

4 files changed

Lines changed: 25 additions & 3 deletions

File tree

helm/kagent/templates/ui-deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ spec:
6464
- name: SSO_REDIRECT_PATH
6565
value: {{ .Values.ui.auth.ssoRedirectPath | default "/oauth2/start" | quote }}
6666
{{- end }}
67+
{{- with .Values.controller.auth.userIdClaim }}
68+
- name: KAGENT_USER_ID_CLAIM
69+
value: {{ . | quote }}
70+
{{- end }}
6771
{{- with .Values.ui.additionalForwardedHeaders }}
6872
- name: KAGENT_ADDITIONAL_FORWARDED_HEADERS
6973
value: {{ join "," . | quote }}

ui/src/app/actions/auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ export async function getCurrentUser(): Promise<CurrentUser | null> {
2828

2929
return claims as CurrentUser;
3030
}
31+
32+
export async function getUserIdClaim(): string {
33+
return process.env.KAGENT_USER_ID_CLAIM || "sub";
34+
}

ui/src/components/AppInitializer.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33
import React, { useEffect, useState } from "react";
44
import { usePathname } from "next/navigation";
55
import { OnboardingWizard } from "./onboarding/OnboardingWizard";
6+
import { useAuth } from "@/contexts/AuthContext";
7+
import { useUserStore } from "@/lib/userStore";
68

79
const LOCAL_STORAGE_KEY = "kagent-onboarding";
810

911
export function AppInitializer({ children }: { children: React.ReactNode }) {
1012
/** `null` = not read yet (must match server + first client paint to avoid hydration mismatch) */
1113
const [isOnboarding, setIsOnboarding] = useState<boolean | null>(null);
1214
const pathname = usePathname();
15+
const { user, userIdClaim } = useAuth();
16+
const setUserId = useUserStore((s) => s.setUserId);
17+
18+
useEffect(() => {
19+
const identity = user?.[userIdClaim] as string | undefined;
20+
if (identity) {
21+
setUserId(identity);
22+
}
23+
}, [user, userIdClaim, setUserId]);
1324

1425
useEffect(() => {
1526
const hasOnboarded = localStorage.getItem(LOCAL_STORAGE_KEY) === "true";

ui/src/contexts/AuthContext.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"use client";
22

33
import React, { createContext, useContext, useEffect, useState, ReactNode } from "react";
4-
import { getCurrentUser, CurrentUser } from "@/app/actions/auth";
4+
import { getCurrentUser, getUserIdClaim, CurrentUser } from "@/app/actions/auth";
55

66
interface AuthContextValue {
77
user: CurrentUser | null;
8+
userIdClaim: string;
89
isLoading: boolean;
910
error: Error | null;
1011
refetch: () => Promise<void>;
@@ -14,15 +15,17 @@ const AuthContext = createContext<AuthContextValue | undefined>(undefined);
1415

1516
export function AuthProvider({ children }: { children: ReactNode }) {
1617
const [user, setUser] = useState<CurrentUser | null>(null);
18+
const [userIdClaim, setUserIdClaim] = useState<string>("sub");
1719
const [isLoading, setIsLoading] = useState(true);
1820
const [error, setError] = useState<Error | null>(null);
1921

2022
const fetchUser = async () => {
2123
setIsLoading(true);
2224
setError(null);
2325
try {
24-
const currentUser = await getCurrentUser();
26+
const [currentUser, claim] = await Promise.all([getCurrentUser(), getUserIdClaim()]);
2527
setUser(currentUser);
28+
setUserIdClaim(claim);
2629
} catch (e) {
2730
setError(e instanceof Error ? e : new Error("Failed to fetch user"));
2831
} finally {
@@ -35,7 +38,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
3538
}, []);
3639

3740
return (
38-
<AuthContext.Provider value={{ user, isLoading, error, refetch: fetchUser }}>
41+
<AuthContext.Provider value={{ user, userIdClaim, isLoading, error, refetch: fetchUser }}>
3942
{children}
4043
</AuthContext.Provider>
4144
);

0 commit comments

Comments
 (0)