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.
This commit is contained in:
padreug 2025-09-05 00:33:29 +02:00
parent 0ee0bc428c
commit 284636dd55

View file

@ -111,15 +111,19 @@ export async function createAppInstance() {
// Install all enabled modules // Install all enabled modules
await pluginManager.installAll() await pluginManager.installAll()
// Set up auth guard // Set up auth guard
router.beforeEach(async (to, _from, next) => { 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') 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('/') next('/')
} else { } else {
console.log(`Auth guard: Allowing navigation to ${to.path}`)
next() next()
} }
}) })