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:
parent
0ee0bc428c
commit
284636dd55
1 changed files with 8 additions and 4 deletions
10
src/app.ts
10
src/app.ts
|
|
@ -113,13 +113,17 @@ export async function createAppInstance() {
|
||||||
|
|
||||||
// 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()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue