@@ -69,7 +69,7 @@ const routes = [
6969 } ,
7070 {
7171 path : '/admin' ,
72- meta : {
72+ meta : {
7373 requiresSetup : true ,
7474 requiresRole : 'global_admin'
7575 } ,
@@ -101,19 +101,37 @@ const router = createRouter({
101101// Navigation guard to check database setup
102102router . beforeEach ( async ( to , from , next ) => {
103103 const databaseStore = useDatabaseStore ( )
104-
104+
105105 // Define public routes that don't need user authentication checks
106106 const publicRoutes = [ 'Setup' , 'Login' , 'Register' ]
107107 const isPublicRoute = publicRoutes . includes ( to . name as string )
108-
108+
109+ // Attempt to get current user status early
110+ let currentUser : any = null ;
111+ try {
112+ // Avoid force refreshing cache here unless necessary, to use existing session info
113+ currentUser = await UserService . getCurrentUser ( ) ;
114+ } catch ( error ) {
115+ console . error ( 'Failed to get current user in guard:' , error ) ;
116+ // currentUser remains null, proceed as unauthenticated for safety
117+ }
118+
119+ // If user is logged in and trying to access Login or Register, redirect to Dashboard
120+ if ( currentUser && ( to . name === 'Login' || to . name === 'Register' ) ) {
121+ next ( '/dashboard' ) ;
122+ return ;
123+ }
124+
109125 // Skip setup check for the setup route itself
110126 if ( to . name === 'Setup' ) {
111127 next ( )
112128 return
113129 }
114130
115- // For public routes (Login/Register), only check database setup, skip user checks
131+ // For public routes (Login/Register) that are NOT being accessed by an already logged-in user
116132 if ( isPublicRoute ) {
133+ // This block is now for genuinely unauthenticated users accessing Login/Register
134+ // or for the Setup page (though Setup is handled above, this keeps structure)
117135 // Check if route requires setup
118136 if ( to . meta . requiresSetup !== false ) {
119137 try {
@@ -132,27 +150,20 @@ router.beforeEach(async (to, from, next) => {
132150 return
133151 }
134152 }
135-
136- // For public routes, proceed without user checks
153+
154+ // For public routes, proceed without further user checks if not already redirected
137155 next ( )
138156 return
139157 }
140158
141- // For protected routes, check user authentication (single call)
142- let currentUser : any = null
143- try {
144- currentUser = await UserService . getCurrentUser ( )
145- } catch ( error ) {
146- console . error ( 'Failed to get current user:' , error )
147- }
148-
149- // If user is logged in and trying to access Login or Register, redirect to Dashboard
150- if ( currentUser && ( to . name === 'Login' || to . name === 'Register' ) ) {
151- next ( '/dashboard' )
152- return
159+ // For protected routes (user is not null or trying to access login/register when logged in)
160+ // If not logged in and trying to access a protected route, redirect to login
161+ if ( ! currentUser && ! isPublicRoute && to . name !== 'Setup' ) {
162+ next ( '/login' ) ;
163+ return ;
153164 }
154165
155- // Check if route requires setup
166+ // Check if route requires setup (for protected routes, currentUser should exist here)
156167 if ( to . meta . requiresSetup !== false ) {
157168 try {
158169 // Check database status (use cache for performance)
@@ -173,8 +184,9 @@ router.beforeEach(async (to, from, next) => {
173184
174185 // Check role requirements (reuse the currentUser from above)
175186 if ( to . meta . requiresRole ) {
187+ // currentUser should be valid here due to the redirect above if null
176188 if ( ! currentUser || currentUser . role_id !== to . meta . requiresRole ) {
177- next ( { name : 'NotFound' } )
189+ next ( { name : 'NotFound' } ) // Or redirect to an 'Unauthorized' page
178190 return
179191 }
180192 }
0 commit comments