@@ -729,9 +729,19 @@ class ApiClient {
729729 // Also set as httpOnly cookie for middleware - encode properly
730730 document . cookie = `jwt_token=${ encodeURIComponent ( cleanToken ) } ; path=/; max-age=${ 7 * 24 * 60 * 60 } ; secure; samesite=strict`
731731 } else {
732+ // Clear all possible token storage locations
732733 localStorage . removeItem ( 'jwt_token' )
733- // Remove cookie
734+ // Remove cookie with all possible variations
734735 document . cookie = 'jwt_token=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;'
736+ document . cookie = 'jwt_token=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT; secure; samesite=strict'
737+ document . cookie = 'jwt_token=; path=/; domain=' + window . location . hostname + '; expires=Thu, 01 Jan 1970 00:00:01 GMT;'
738+
739+ // Also clear sessionStorage just in case
740+ try {
741+ sessionStorage . removeItem ( 'jwt_token' )
742+ } catch ( e ) {
743+ console . warn ( 'Could not clear sessionStorage:' , e )
744+ }
735745 }
736746 }
737747 }
@@ -865,25 +875,29 @@ class ApiClient {
865875 // For public endpoints, don't clear token and use specific error message
866876 throw new Error ( errorData . message || 'Hitelesítési hiba történt.' )
867877 } else {
868- // For protected endpoints, be more careful about clearing tokens
869- // Only clear token if it's explicitly an authentication error
870- if ( errorData . message && (
871- errorData . message . includes ( 'Invalid token' ) ||
872- errorData . message . includes ( 'Token expired' ) ||
873- errorData . message . includes ( 'Authentication failed' ) ||
874- errorData . message . includes ( 'Hitelesítés sikertelen' )
875- ) ) {
876- console . log ( '🔑 Clearing token due to explicit auth error:' , errorData . message )
877- this . setToken ( null )
878- throw new Error ( 'A munkamenet lejárt. Kérjük, jelentkezzen be újra.' )
879- } else {
880- // For other 401 errors, don't clear token - could be temporary issue
881- console . warn ( '⚠️ 401 error but preserving token (might be temporary):' , {
882- endpoint,
883- error : errorData . message
884- } )
885- throw new Error ( errorData . message || 'Hitelesítési hiba történt. Próbálja újra.' )
878+ // For protected endpoints, always clear expired tokens
879+ // Be more aggressive about token clearing on 401 errors in production
880+ // This fixes the redirect loop issue where expired tokens weren't being cleared properly
881+ console . log ( '🔑 401 error on protected endpoint - clearing token:' , {
882+ endpoint,
883+ error : errorData . message ,
884+ environment : typeof window !== 'undefined' ? window . location . hostname : 'unknown'
885+ } )
886+
887+ // Always clear token on 401 for protected endpoints to prevent redirect loops
888+ // When a token truly expires, we need to ensure it's completely removed from all storage
889+ this . setToken ( null )
890+
891+ // Force reload to clear any cached state and redirect properly
892+ // This prevents the middleware redirect loop between /login and /app/iranyitopult
893+ if ( typeof window !== 'undefined' ) {
894+ // Small delay to ensure token clearing is completed
895+ setTimeout ( ( ) => {
896+ window . location . href = '/login'
897+ } , 100 )
886898 }
899+
900+ throw new Error ( 'A munkamenet lejárt. Kérjük, jelentkezzen be újra.' )
887901 }
888902 } else if ( response . status === 403 ) {
889903 throw new Error ( 'Nincs jogosultsága ehhez a művelethez.' )
@@ -980,9 +994,11 @@ class ApiClient {
980994 }
981995
982996 private shouldNotRetry ( error : Error ) : boolean {
983- // Don't retry on authentication errors
997+ // Don't retry on authentication errors - token is likely expired
984998 if ( error . message . includes ( '401' ) || error . message . includes ( 'Unauthorized' ) ||
985- error . message . includes ( 'munkamenet lejárt' ) ) {
999+ error . message . includes ( 'munkamenet lejárt' ) || error . message . includes ( 'Hitelesítési hiba' ) ||
1000+ error . message . includes ( 'Authentication' ) || error . message . includes ( 'Invalid token' ) ||
1001+ error . message . includes ( 'Token expired' ) ) {
9861002 return true
9871003 }
9881004
0 commit comments