From e53f36492924116adfc9bf0987b8b00cde8ea1e9 Mon Sep 17 00:00:00 2001 From: "bert.hausmans" Date: Tue, 14 Apr 2026 16:36:43 +0200 Subject: [PATCH] fix: guard against Pinia not ready in admin router (plugin load order) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The admin app's 1.router plugin loads before 2.pinia. During router.install(), the initial navigation triggers the index redirect and beforeEach guard, both of which call useAuthStore() — but Pinia isn't registered yet, causing a crash. Fix: wrap useAuthStore() in try/catch in both additional-routes.ts and guards.ts. On the initial router install navigation, the catch falls back to redirecting to login / allowing navigation. Once Pinia is available on subsequent navigations, the store works normally. Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/admin/src/plugins/1.router/additional-routes.ts | 10 +++++++++- apps/admin/src/plugins/1.router/guards.ts | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/admin/src/plugins/1.router/additional-routes.ts b/apps/admin/src/plugins/1.router/additional-routes.ts index 8127d5f3..7d72c5c0 100644 --- a/apps/admin/src/plugins/1.router/additional-routes.ts +++ b/apps/admin/src/plugins/1.router/additional-routes.ts @@ -11,7 +11,15 @@ export const redirects: RouteRecordRaw[] = [ path: '/', name: 'index', redirect: to => { - const authStore = useAuthStore() + // This redirect fires during router.install(), which runs before + // Pinia is registered (1.router < 2.pinia). Guard against that. + let authStore + try { + authStore = useAuthStore() + } + catch { + return { name: 'login', query: to.query } + } if (!authStore.isAuthenticated) return { name: 'login', query: to.query } diff --git a/apps/admin/src/plugins/1.router/guards.ts b/apps/admin/src/plugins/1.router/guards.ts index c74f1cb3..bfec9afb 100644 --- a/apps/admin/src/plugins/1.router/guards.ts +++ b/apps/admin/src/plugins/1.router/guards.ts @@ -6,7 +6,15 @@ export const setupGuards = ( router: _RouterTyped ) => { router.beforeEach(async (to, from) => { - const authStore = useAuthStore(); + // Guard fires during router.install() before Pinia is registered + // (1.router loads before 2.pinia). Skip auth checks on that first call. + let authStore; + try { + authStore = useAuthStore(); + } + catch { + return; + } // Wait for initialization to complete (only blocks on first navigation) if (!authStore.isInitialized) {