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
12
src/app.ts
12
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()
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue