fix: guard against Pinia not ready in admin router (plugin load order)

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) <noreply@anthropic.com>
This commit is contained in:
2026-04-14 16:36:43 +02:00
parent 887a8fccde
commit e53f364929
2 changed files with 18 additions and 2 deletions

View File

@@ -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 }

View File

@@ -6,7 +6,15 @@ export const setupGuards = (
router: _RouterTyped<RouteNamedMap & { [key: string]: any }>
) => {
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) {