silentLogout function

Future<bool> silentLogout()

Clear all login state without opening a browser.

Performs the same cleanup as logoutPod but invalidates the IdP session via a headless HTTP request rather than launching a visible browser window. Use this when switching accounts or recovering from stale credentials.

Implementation

Future<bool> silentLogout() async {
  try {
    await KeyManager.clear();

    // Get logout URL from discovery doc before clearing the manager.

    String? logoutUrl;
    try {
      logoutUrl = await AuthDataManager.getLogoutUrl();
    } on Object catch (_) {}

    // Clear local token state only — no browser redirect.

    await AuthDataManager.getAuthManager()?.forgetUser();

    final authDataRemoved = await AuthDataManager.removeAuthData();

    if (_onLogoutClearCaches != null) {
      try {
        await _onLogoutClearCaches!();
      } on Object catch (e) {
        debugPrint('silentLogout() cache callback failed (non-critical): $e');
      }
    }

    // Best-effort IdP session invalidation via headless HTTP GET.

    if (logoutUrl != null && logoutUrl.isNotEmpty) {
      try {
        await http.get(Uri.parse(logoutUrl));
      } on Object catch (e) {
        debugPrint('silentLogout() headless logout failed (non-critical): $e');
      }
    }

    return authDataRemoved;
  } on Object catch (e) {
    debugPrint('silentLogout() CRITICAL: $e');
    try {
      await AuthDataManager.removeAuthData();
      await KeyManager.clear();
    } on Object catch (_) {}
    return false;
  }
}