@@ -13,9 +13,9 @@ export function AuthProvider({ children }: AuthProviderProps) {
1313 const { isAuthenticated, accessToken, refreshUser, clearAuth } =
1414 useAuthStore ( ) ;
1515
16- // Handle store hydration
16+
1717 useEffect ( ( ) => {
18- // Check if already hydrated
18+
1919 if ( useAuthStore . persist . hasHydrated ( ) ) {
2020 setIsHydrated ( true ) ;
2121 return ;
@@ -25,7 +25,7 @@ export function AuthProvider({ children }: AuthProviderProps) {
2525 setIsHydrated ( true ) ;
2626 } ) ;
2727
28- // Fallback timeout to prevent infinite loading
28+
2929 const timeout = setTimeout ( ( ) => {
3030 setIsHydrated ( true ) ;
3131 } , 2000 ) ;
@@ -36,21 +36,21 @@ export function AuthProvider({ children }: AuthProviderProps) {
3636 } ;
3737 } , [ ] ) ;
3838
39- // Initialize auth state on mount
39+
4040 useEffect ( ( ) => {
4141 if ( ! isHydrated ) return ;
4242
4343 const initializeAuth = async ( ) => {
4444 try {
45- // If we have a token but no user data, try to refresh
45+
4646 if ( accessToken && isAuthenticated ) {
4747 await refreshUser ( ) ;
4848 } else if ( accessToken ) {
49- // Try to refresh user data to validate token
49+
5050 try {
5151 await refreshUser ( ) ;
5252 } catch {
53- // If refresh fails, clear auth data
53+
5454 clearAuth ( ) ;
5555 }
5656 }
@@ -62,15 +62,15 @@ export function AuthProvider({ children }: AuthProviderProps) {
6262 initializeAuth ( ) ;
6363 } , [ isHydrated , accessToken , isAuthenticated , refreshUser , clearAuth ] ) ;
6464
65- // Show loading state while hydrating
65+
6666 if ( ! isHydrated ) {
6767 return < Loading /> ;
6868 }
6969
7070 return < > { children } </ > ;
7171}
7272
73- // Hook to check if auth is hydrated
73+
7474export function useAuthHydration ( ) {
7575 const [ isHydrated , setIsHydrated ] = useState ( false ) ;
7676
@@ -85,7 +85,6 @@ export function useAuthHydration() {
8585 return isHydrated ;
8686}
8787
88- // Component to show loading while auth is initializing
8988export function AuthLoadingProvider ( {
9089 children,
9190} : {
0 commit comments