diff --git a/apps/app/src/plugins/3.axios-bindings.ts b/apps/app/src/plugins/3.axios-bindings.ts new file mode 100644 index 00000000..599a1793 --- /dev/null +++ b/apps/app/src/plugins/3.axios-bindings.ts @@ -0,0 +1,26 @@ +import type { App } from 'vue' +import { apiClient, registerInterceptors } from '@/lib/axios' +import { useAuthStore } from '@/stores/useAuthStore' +import { useImpersonationStore } from '@/stores/useImpersonationStore' +import { useNotificationStore } from '@/stores/useNotificationStore' +import { useOrganisationStore } from '@/stores/useOrganisationStore' + +// Numeric prefix `3.` runs after `2.pinia.ts`, so Pinia is active by +// the time these store factories resolve. Stores are looked up lazily +// inside each callback (not eagerly at plugin-init), which keeps the +// seam tolerant of any future plugin-ordering changes. +export default function (_: App): void { + registerInterceptors(apiClient, { + getActiveOrgId: () => useOrganisationStore().activeOrganisationId, + notify: (message, level) => useNotificationStore().show(message, level), + onAuthFail: () => { + const authStore = useAuthStore() + if (authStore.isInitialized) + authStore.handleUnauthorized() + }, + onImpersonationRevoked: () => { + useImpersonationStore().clearState() + window.location.href = '/platform' + }, + }) +}