From 66951086fb7498689997e4ce0f2d5dbf366aa977 Mon Sep 17 00:00:00 2001 From: kitos Date: Wed, 22 Jul 2026 09:41:52 +0200 Subject: [PATCH] fix(auth): refresh cached user after self-role edit so RoleSwitcher shows up extra_roles changes made to the currently-logged-in admin's own account weren't reflected until logout/login, since AuthContext only fetches /users/me once at mount. Now refetches it when the edited user is the caller themselves. --- frontend/src/context/AuthContext.tsx | 5 +++++ frontend/src/pages/UsersPage.tsx | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/frontend/src/context/AuthContext.tsx b/frontend/src/context/AuthContext.tsx index 5b9715a..c717c8c 100644 --- a/frontend/src/context/AuthContext.tsx +++ b/frontend/src/context/AuthContext.tsx @@ -28,6 +28,10 @@ interface AuthState { * Reloads the page afterward so every role-gated component (nav, route * guards, etc.) re-evaluates cleanly against the new role. */ switchRole: (role: string) => Promise; + /** Re-fetch the current user from /users/me — call after an admin edits + * the logged-in user's own role/extra_roles so gated UI (e.g. the role + * switcher) picks it up without requiring a logout/login. */ + refreshUser: () => Promise; } const AuthContext = createContext(undefined); @@ -93,6 +97,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { login, logout, switchRole, + refreshUser, }} > {children} diff --git a/frontend/src/pages/UsersPage.tsx b/frontend/src/pages/UsersPage.tsx index ae344e5..3aef9a3 100644 --- a/frontend/src/pages/UsersPage.tsx +++ b/frontend/src/pages/UsersPage.tsx @@ -20,6 +20,7 @@ import { type UserCreatePayload, } from "../api/users"; import { useToast } from "../components/Toast"; +import { useAuth } from "../context/AuthContext"; const ROLES = [ { value: "viewer", label: "Viewer" }, @@ -44,6 +45,7 @@ const roleBadgeColors: Record = { export default function UsersPage() { const queryClient = useQueryClient(); const { showToast } = useToast(); + const { user: currentUser, refreshUser } = useAuth(); const [showCreateModal, setShowCreateModal] = useState(false); const [editingUser, setEditingUser] = useState(null); @@ -67,8 +69,12 @@ export default function UsersPage() { const updateMutation = useMutation({ mutationFn: ({ userId, payload }: { userId: string; payload: Parameters[1] }) => updateUser(userId, payload), - onSuccess: () => { + onSuccess: (_data, { userId }) => { queryClient.invalidateQueries({ queryKey: ["users"] }); + // Editing your own role/extra_roles wouldn't otherwise show up until + // a logout/login — refresh the cached auth user so gated UI (e.g. + // the role switcher) picks it up immediately. + if (userId === currentUser?.id) refreshUser(); setEditingUser(null); }, });