refactor: replace useQuery with useAuthQuery for improved authentication handling

- Updated multiple components to utilize useAuthQuery instead of useQuery for fetching user-related data, enhancing authentication management.
- Adjusted balance and subscription queries across various components including InitUser, ManageSubscription, PricingCards, CreditDisplay, and others to ensure consistent authentication checks.
- Improved overall code maintainability by centralizing authentication logic in the new hook.
This commit is contained in:
Matthias
2026-03-27 18:35:12 +01:00
parent 2f89465e82
commit 6e38e2d270
9 changed files with 48 additions and 25 deletions

24
hooks/use-auth-query.ts Normal file
View File

@@ -0,0 +1,24 @@
"use client";
import { useConvexAuth, useQuery } from "convex/react";
import type { FunctionReference, FunctionArgs, FunctionReturnType } from "convex/server";
/**
* Wrapper um `useQuery` der automatisch `"skip"` nutzt wenn der
* Convex-Auth-Token noch nicht bereit ist. Verhindert "Unauthenticated"-Fehler
* bei Queries die `requireAuth` nutzen.
*
* Nutzt nur `isAuthenticated` (nicht `isLoading`) als Guard — wenn ein
* `initialToken` vom SSR vorhanden ist, springt `isAuthenticated` sofort
* auf `true` ohne Loading-Phase, sodass Queries ohne Verzögerung feuern.
*/
export function useAuthQuery<F extends FunctionReference<"query">>(
query: F,
...args: [] | [FunctionArgs<F> | "skip"]
): FunctionReturnType<F> | undefined {
const { isAuthenticated } = useConvexAuth();
const shouldSkip = !isAuthenticated || args[0] === "skip";
return useQuery(query, shouldSkip ? "skip" : (args[0] ?? ({} as FunctionArgs<F>)));
}