From fe414edf71296f129960df61818c19a3897dae08 Mon Sep 17 00:00:00 2001 From: Trezy Date: Sun, 22 Mar 2026 16:20:18 -0500 Subject: [PATCH] fix: prevent permission redirects before permissions are known --- web/src/app/dashboard/lexicons/new/page.tsx | 8 ++++---- web/src/hooks/use-current-user.ts | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/web/src/app/dashboard/lexicons/new/page.tsx b/web/src/app/dashboard/lexicons/new/page.tsx index f48a8f8..feb6ccf 100644 --- a/web/src/app/dashboard/lexicons/new/page.tsx +++ b/web/src/app/dashboard/lexicons/new/page.tsx @@ -20,17 +20,17 @@ import { Switch } from "@/components/ui/switch"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; export default function AddLexiconPage() { - const { hasPermission } = useCurrentUser(); + const { hasPermission, loading } = useCurrentUser(); const router = useRouter(); const [error, setError] = useState(null); const [submitting, setSubmitting] = useState(false); - // Redirect if the user cannot create lexicons + // Redirect if the user cannot create lexicons (only after loading completes) useEffect(() => { - if (!hasPermission("lexicons:create")) { + if (!loading && !hasPermission("lexicons:create")) { router.replace("/dashboard/lexicons"); } - }, [hasPermission, router]); + }, [hasPermission, loading, router]); // Local state const [json, setJson] = useState(LEXICON_TEMPLATE); diff --git a/web/src/hooks/use-current-user.ts b/web/src/hooks/use-current-user.ts index fea9bd1..fc3f9c6 100644 --- a/web/src/hooks/use-current-user.ts +++ b/web/src/hooks/use-current-user.ts @@ -7,11 +7,14 @@ import type { UserSummary } from "@/types/users"; export function useCurrentUser() { const { did } = useAuth(); const [currentUser, setCurrentUser] = useState(null); + const [loading, setLoading] = useState(true); const load = useCallback(() => { + setLoading(true); getUsers() .then((users) => setCurrentUser(users.find((u) => u.did === did) ?? null)) - .catch(() => setCurrentUser(null)); + .catch(() => setCurrentUser(null)) + .finally(() => setLoading(false)); }, [did]); useEffect(() => { @@ -26,5 +29,5 @@ export function useCurrentUser() { [currentUser, isSuper], ); - return { currentUser, isSuper, hasPermission, reload: load }; + return { currentUser, isSuper, hasPermission, loading, reload: load }; } -- 2.51.2