// Access the provided Zitadel auth instance + a small JSON helper for /api calls. // `auth.fetch` auto-attaches the Bearer access token. export function useAuth() { const { $auth } = useNuxtApp() as unknown as { $auth: { isAuthenticated: Ref; isLoading: Ref; user: Ref<{ sub: string; name: string; email: string } | null>; error: Ref; login: () => void; logout: () => Promise; fetch: (url: string, init?: RequestInit) => Promise; }; }; async function api(path: string, init?: RequestInit): Promise { const res = await $auth.fetch(`/api${path}`, init); if (!res.ok) { let message = `${res.status}`; try { const body = await res.json(); message = body?.statusMessage || body?.message || message; } catch { // non-JSON error body } throw new Error(message); } if (res.status === 204) return undefined as T; return (await res.json()) as T; } return { auth: $auth, api }; }