diff --git a/app/composables/useAuth.ts b/app/composables/useAuth.ts index b1bfbd8..a11bca6 100644 --- a/app/composables/useAuth.ts +++ b/app/composables/useAuth.ts @@ -1,17 +1,10 @@ -// Access the provided Zitadel auth instance + a small JSON helper for /api calls. +import type { ZitadelAuth } from "@kuns/zitadel-auth"; + +// Access the bootstrap-provided Zitadel auth instance + a small JSON helper for /api calls. +// By the time any component mounts, the plugin has gated auth, so `auth` is authenticated. // `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; - }; - }; + const { $auth } = useNuxtApp() as unknown as { $auth: ZitadelAuth }; async function api(path: string, init?: RequestInit): Promise { const res = await $auth.fetch(`/api${path}`, init); diff --git a/app/pages/auth/callback.vue b/app/pages/auth/callback.vue index 43a557a..3160815 100644 --- a/app/pages/auth/callback.vue +++ b/app/pages/auth/callback.vue @@ -1,11 +1,8 @@ diff --git a/app/pages/index.vue b/app/pages/index.vue index 8dd5eed..5ffd6f6 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -86,15 +86,20 @@ async function addTask() { } } -onMounted(refreshLists); +onMounted(() => { + // The auth plugin gates before mount, so this is normally authenticated. + // Safety net: if not, drive login instead of calling the API (no 401 banner). + if (auth.isAuthenticated) refreshLists(); + else auth.login(); +});