Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions helm/kagent/templates/ui-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ spec:
- name: SSO_REDIRECT_PATH
value: {{ .Values.ui.auth.ssoRedirectPath | default "/oauth2/start" | quote }}
{{- end }}
{{- with .Values.controller.auth.userIdClaim }}
- name: KAGENT_USER_ID_CLAIM
value: {{ . | quote }}
{{- end }}
{{- with .Values.ui.additionalForwardedHeaders }}
- name: KAGENT_ADDITIONAL_FORWARDED_HEADERS
value: {{ join "," . | quote }}
Expand Down
4 changes: 4 additions & 0 deletions ui/src/app/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ export async function getCurrentUser(): Promise<CurrentUser | null> {

return claims as CurrentUser;
}

export async function getUserIdClaim(): Promise<string> {
return process.env.KAGENT_USER_ID_CLAIM || "sub";
}
11 changes: 11 additions & 0 deletions ui/src/components/AppInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
import React, { useEffect, useState } from "react";
import { usePathname } from "next/navigation";
import { OnboardingWizard } from "./onboarding/OnboardingWizard";
import { useAuth } from "@/contexts/AuthContext";
import { useUserStore } from "@/lib/userStore";

const LOCAL_STORAGE_KEY = "kagent-onboarding";

export function AppInitializer({ children }: { children: React.ReactNode }) {
/** `null` = not read yet (must match server + first client paint to avoid hydration mismatch) */
const [isOnboarding, setIsOnboarding] = useState<boolean | null>(null);
const pathname = usePathname();
const { user, userIdClaim } = useAuth();
const setUserId = useUserStore((s) => s.setUserId);

useEffect(() => {
const identity = user?.[userIdClaim] as string | undefined;
if (identity) {
setUserId(identity);
}
}, [user, userIdClaim, setUserId]);

useEffect(() => {
const hasOnboarded = localStorage.getItem(LOCAL_STORAGE_KEY) === "true";
Expand Down
9 changes: 6 additions & 3 deletions ui/src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client";

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

interface AuthContextValue {
user: CurrentUser | null;
userIdClaim: string;
isLoading: boolean;
error: Error | null;
refetch: () => Promise<void>;
Expand All @@ -14,15 +15,17 @@ const AuthContext = createContext<AuthContextValue | undefined>(undefined);

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

const fetchUser = async () => {
setIsLoading(true);
setError(null);
try {
const currentUser = await getCurrentUser();
const [currentUser, claim] = await Promise.all([getCurrentUser(), getUserIdClaim()]);
setUser(currentUser);
setUserIdClaim(claim);
} catch (e) {
setError(e instanceof Error ? e : new Error("Failed to fetch user"));
} finally {
Expand All @@ -35,7 +38,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
}, []);

return (
<AuthContext.Provider value={{ user, isLoading, error, refetch: fetchUser }}>
<AuthContext.Provider value={{ user, userIdClaim, isLoading, error, refetch: fetchUser }}>
{children}
</AuthContext.Provider>
);
Expand Down
Loading