From 284636dd5543022cc6e22945e667c172ca3f8c28 Mon Sep 17 00:00:00 2001 From: padreug Date: Fri, 5 Sep 2025 00:33:29 +0200 Subject: [PATCH] Refactor authentication logic in app routing for improved clarity and performance - Simplify the authentication guard by directly using the auth composable for user authentication checks. - Enhance logging within the auth guard to provide better feedback during navigation based on authentication status. - Remove unnecessary dependency on the auth service, streamlining the code for better maintainability. --- src/app.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/app.ts b/src/app.ts index 026f288..2525c16 100644 --- a/src/app.ts +++ b/src/app.ts @@ -111,15 +111,19 @@ export async function createAppInstance() { // Install all enabled modules await pluginManager.installAll() - // Set up auth guard + // Set up auth guard router.beforeEach(async (to, _from, next) => { - const authService = container.inject(SERVICE_TOKENS.AUTH_SERVICE) as any + // Import the auth composable to ensure we're using the same instance as the rest of the app + const { auth } = await import('@/composables/useAuth') - if (to.meta.requiresAuth && authService && !authService.isAuthenticated?.value) { + if (to.meta.requiresAuth && !auth.isAuthenticated.value) { + console.log('Auth guard: User not authenticated, redirecting to login') next('/login') - } else if (to.path === '/login' && authService && authService.isAuthenticated?.value) { + } else if (to.path === '/login' && auth.isAuthenticated.value) { + console.log('Auth guard: User already authenticated, redirecting to home') next('/') } else { + console.log(`Auth guard: Allowing navigation to ${to.path}`) next() } })