diff --git a/luckychit/lib/core/services/api_service.dart b/luckychit/lib/core/services/api_service.dart index 3916f9a..3d92a2b 100644 --- a/luckychit/lib/core/services/api_service.dart +++ b/luckychit/lib/core/services/api_service.dart @@ -41,9 +41,17 @@ static const String tokenKey = 'auth_token'; onError: (error, handler) { print('❌ [ApiService] Error: ${error.response?.statusCode} - ${error.message}'); if (error.response?.statusCode == 401) { - print('❌ [ApiService] 401 Unauthorized - Clearing token'); - // Token expired or invalid - _clearToken(); + print('❌ [ApiService] 401 Unauthorized detected'); + // Only clear token if the error message indicates token is expired/invalid + final errorMessage = error.response?.data?['message']?.toString() ?? ''; + if (errorMessage.contains('Token expired') || + errorMessage.contains('Invalid token') || + errorMessage.contains('Authentication required')) { + print('❌ [ApiService] Token is invalid/expired - Clearing token'); + _clearToken(); + } else { + print('⚠️ [ApiService] 401 but token might be valid - NOT clearing (backend issue?)'); + } } handler.next(error); },