1- import { useEffect } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22
33import { Outlet } from '@tanstack/react-router' ;
44
@@ -11,9 +11,13 @@ import { useAppStore } from '@/store/store';
1111
1212export function App ( ) {
1313 const { user, isAuthenticated, isLoading, loginWithRedirect } = useAuth ( ) ;
14- const { mutate : fetchUserDetails } = useGetUserDetailsMutation ( ) ;
14+ const { mutate : fetchUserDetails , isPending : isFetchingUserDetails } =
15+ useGetUserDetailsMutation ( ) ;
1516 const { setUserDetail } = useAppStore ( ) ;
1617
18+ const [ userDetailsFetched , setUserDetailsFetched ] = useState ( false ) ;
19+ const [ fetchInitiated , setFetchInitiated ] = useState ( false ) ;
20+
1721 useEffect ( ( ) => {
1822 Logger . logEvent ( 'AppStarted' , {
1923 startTime : new Date ( ) . toISOString ( ) ,
@@ -46,6 +50,7 @@ export function App() {
4650 window . removeEventListener ( 'unhandledrejection' , handleUnhandledRejection ) ;
4751 } ;
4852 } , [ ] ) ;
53+
4954 // Handle authentication redirect
5055 useEffect ( ( ) => {
5156 if ( ! isLoading && ! isAuthenticated ) {
@@ -61,34 +66,63 @@ export function App() {
6166 returnTo : window . location . pathname + window . location . search ,
6267 } ,
6368 } ) ;
64- } else if ( isAuthenticated && user ?. email ) {
69+ } else if ( isAuthenticated && user ?. email && ! fetchInitiated ) {
6570 // Log successful authentication
6671 Logger . logEvent ( 'UserAuthenticated' , {
6772 userId : user . sub ,
6873 userEmail : user . email ,
6974 timestamp : new Date ( ) . toISOString ( ) ,
7075 } ) ;
76+
77+ // Mark that we've initiated the fetch
78+ setFetchInitiated ( true ) ;
79+
7180 // Fetch user details
7281 fetchUserDetails ( user . email , {
7382 onSuccess : userDetails => {
7483 setUserDetail ( {
75- id : typeof userDetails . id === 'number' ? userDetails . id : 0 ,
76- email : userDetails . email || '' ,
77- username : userDetails . username || '' ,
78- role : typeof userDetails . role === 'number' ? userDetails . role : 0 ,
79- organization :
80- typeof userDetails . organization === 'number' ? userDetails . organization : 0 ,
84+ id : userDetails . id ,
85+ email : userDetails . email ,
86+ username : userDetails . username ,
87+ role : userDetails . role ,
88+ organization : userDetails . organization ,
8189 firstName : userDetails . firstName ,
8290 lastName : userDetails . lastName ,
8391 status : userDetails . status ,
8492 } ) ;
93+ setUserDetailsFetched ( true ) ;
8594 } ,
8695 onError : error => {
8796 console . error ( 'Failed to fetch user details:' , error ) ;
97+
98+ // Log the error
99+ Logger . logException ( error instanceof Error ? error : new Error ( String ( error ) ) , {
100+ source : 'FetchUserDetails' ,
101+ userEmail : user . email ,
102+ } ) ;
103+
104+ // Reset states and redirect to login on error
105+ setFetchInitiated ( false ) ;
106+ setUserDetailsFetched ( false ) ;
107+
108+ // Redirect to login
109+ void loginWithRedirect ( {
110+ appState : {
111+ returnTo : window . location . pathname + window . location . search ,
112+ } ,
113+ } ) ;
88114 } ,
89115 } ) ;
90116 }
91- } , [ isAuthenticated , isLoading , loginWithRedirect , user , fetchUserDetails , setUserDetail ] ) ;
117+ } , [
118+ isAuthenticated ,
119+ isLoading ,
120+ loginWithRedirect ,
121+ user ,
122+ fetchUserDetails ,
123+ setUserDetail ,
124+ fetchInitiated ,
125+ ] ) ;
92126
93127 if ( isLoading ) {
94128 return (
@@ -116,6 +150,19 @@ export function App() {
116150 ) ;
117151 }
118152
153+ if ( isFetchingUserDetails || ! userDetailsFetched ) {
154+ return (
155+ < ErrorBoundary >
156+ < div className = 'flex h-screen items-center justify-center bg-gray-50' >
157+ < div className = 'text-center' >
158+ < div className = 'mx-auto mb-4 h-12 w-12 animate-spin rounded-full border-b-2 border-blue-600' > </ div >
159+ < p className = 'text-lg text-gray-600' > Loading user details...</ p >
160+ </ div >
161+ </ div >
162+ </ ErrorBoundary >
163+ ) ;
164+ }
165+
119166 return (
120167 < ErrorBoundary >
121168 < div className = 'flex h-screen flex-col overflow-hidden' >
0 commit comments