fix(auth): refresh cached user after self-role edit so RoleSwitcher shows up
Aegis CI / lint-and-test (push) Has been cancelled
Snyk Security Scan / Python vulnerabilities (backend) (push) Has been cancelled
Snyk Security Scan / npm vulnerabilities (frontend) (push) Has been cancelled
Snyk Security Scan / Docker image vulnerabilities (backend) (push) Has been cancelled

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.
This commit is contained in:
kitos
2026-07-22 09:41:52 +02:00
parent 06c913955c
commit 66951086fb
2 changed files with 12 additions and 1 deletions
+5
View File
@@ -28,6 +28,10 @@ interface AuthState {
* Reloads the page afterward so every role-gated component (nav, route * Reloads the page afterward so every role-gated component (nav, route
* guards, etc.) re-evaluates cleanly against the new role. */ * guards, etc.) re-evaluates cleanly against the new role. */
switchRole: (role: string) => Promise<void>; switchRole: (role: string) => Promise<void>;
/** 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<void>;
} }
const AuthContext = createContext<AuthState | undefined>(undefined); const AuthContext = createContext<AuthState | undefined>(undefined);
@@ -93,6 +97,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
login, login,
logout, logout,
switchRole, switchRole,
refreshUser,
}} }}
> >
{children} {children}
+7 -1
View File
@@ -20,6 +20,7 @@ import {
type UserCreatePayload, type UserCreatePayload,
} from "../api/users"; } from "../api/users";
import { useToast } from "../components/Toast"; import { useToast } from "../components/Toast";
import { useAuth } from "../context/AuthContext";
const ROLES = [ const ROLES = [
{ value: "viewer", label: "Viewer" }, { value: "viewer", label: "Viewer" },
@@ -44,6 +45,7 @@ const roleBadgeColors: Record<string, string> = {
export default function UsersPage() { export default function UsersPage() {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { showToast } = useToast(); const { showToast } = useToast();
const { user: currentUser, refreshUser } = useAuth();
const [showCreateModal, setShowCreateModal] = useState(false); const [showCreateModal, setShowCreateModal] = useState(false);
const [editingUser, setEditingUser] = useState<UserOut | null>(null); const [editingUser, setEditingUser] = useState<UserOut | null>(null);
@@ -67,8 +69,12 @@ export default function UsersPage() {
const updateMutation = useMutation({ const updateMutation = useMutation({
mutationFn: ({ userId, payload }: { userId: string; payload: Parameters<typeof updateUser>[1] }) => mutationFn: ({ userId, payload }: { userId: string; payload: Parameters<typeof updateUser>[1] }) =>
updateUser(userId, payload), updateUser(userId, payload),
onSuccess: () => { onSuccess: (_data, { userId }) => {
queryClient.invalidateQueries({ queryKey: ["users"] }); 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); setEditingUser(null);
}, },
}); });