diff --git a/lib/features/auth/presentation/providers/auth_provider.dart b/lib/features/auth/presentation/providers/auth_provider.dart index fa7bdd3..e18d1d6 100644 --- a/lib/features/auth/presentation/providers/auth_provider.dart +++ b/lib/features/auth/presentation/providers/auth_provider.dart @@ -75,7 +75,10 @@ class AuthNotifier extends StateNotifier { // when token refresh fails ApiClient.onForceLogout = () { SocketService().disconnect(); - state = const AuthState(); + // Only reset if still authenticated — avoid showing errors on login screen + if (state.status == AuthStatus.authenticated) { + state = const AuthState(); + } }; checkAuthStatus(); } @@ -107,16 +110,16 @@ class AuthNotifier extends StateNotifier { } Future logout() async { + // Reset state first so UI navigates to login immediately + state = const AuthState(); // Disconnect socket immediately SocketService().disconnect(); - // Attempt FCM unregister and API logout (best-effort, don't block) + // Clear tokens BEFORE any API calls to prevent 401 errors during logout + await SecureStorage.clearTokens(); + // Best-effort cleanup — tokens already cleared, ignore any errors try { await PushNotificationService().unregister(); } catch (_) {} - try { - await _repository.logout(); - } catch (_) {} - state = const AuthState(); } Future login({