logoutInteractive method

Future<LogoutResult> logoutInteractive({
  1. required BuildContext context,
  2. required String title,
  3. OpenIdIdentity? identity,
  4. String? userNameHint,
  5. Map<String, String>? additionalParameters,
  6. Iterable<String>? prompts,
  7. bool useWebPopup = true,
  8. int popupWidth = 640,
  9. int popupHeight = 600,
  10. String? postLogoutRedirectUri,
  11. bool useBasicAuth = true,
})

Performs RP-initiated logout when supported, while also revoking tokens and clearing persisted identity state for that session.

Pass identity to use a previously captured snapshot. When omitted, the currently loaded identity is snapshotted at call time.

Local identity is cleared only when it still belongs to the session that started this logout. Remote logout/revocation failures are logged and returned as LogoutStatus.remoteFailure instead of blocking local cleanup. RFC 7009 unsupported_token_type is treated as a successful skip for that token.

Implementation

Future<LogoutResult> logoutInteractive({
  required BuildContext context,
  required String title,
  OpenIdIdentity? identity,
  String? userNameHint,
  Map<String, String>? additionalParameters,
  Iterable<String>? prompts,
  bool useWebPopup = true,
  int popupWidth = 640,
  int popupHeight = 600,
  String? postLogoutRedirectUri,
  bool useBasicAuth = true,
}) async {
  final generation = _sessionGeneration;
  final snapshot = await _identitySnapshot(identity);
  if (snapshot == null) {
    const result = LogoutResult(
      status: LogoutStatus.skipped,
      message: 'Interactive logout skipped because no identity is loaded.',
    );
    openIdConnectLogger.i(result.message);
    return result;
  }

  final shouldClearLocal = _shouldClearIdentity(snapshot);
  if (shouldClearLocal) {
    _cancelAutoRenewTimer();
  }

  return _completeLogout(
    generation: generation,
    shouldClearLocal: shouldClearLocal,
    remoteLogout: () async {
      await _verifyDiscoveryDocument();
      final endSession = configuration?.endSessionEndpoint;

      if (endSession == null) {
        openIdConnectLogger.i(
          'Provider does not advertise an end-session endpoint; revoking tokens locally.',
        );
        await revokeTokens(identity: snapshot, useBasicAuth: useBasicAuth);
        return null;
      }

      final request = InteractiveLogoutRequest(
        configuration: configuration!,
        postLogoutRedirectUrl:
            postLogoutRedirectUri ??
            redirectUrl ??
            (throw StateError(
              'When using logout interactive, you must provide a postLogoutRedirectUri or create the client with a redirect url.',
            )),
        useWebPopup: useWebPopup,
        popupHeight: popupHeight,
        popupWidth: popupWidth,
        idToken: snapshot.idToken,
      );

      if (!context.mounted) {
        throw StateError(
          'Interactive logout aborted because the calling context is no longer mounted.',
        );
      }

      if (kIsWeb) {
        final response = await OpenIdConnect.logoutInteractive(
          context: context,
          title: title,
          request: request,
        );
        await revokeTokens(identity: snapshot, useBasicAuth: useBasicAuth);
        return response;
      }

      await revokeTokens(identity: snapshot, useBasicAuth: useBasicAuth);
      if (!context.mounted) {
        throw StateError(
          'Interactive logout aborted after token revocation because the calling context is no longer mounted.',
        );
      }

      return OpenIdConnect.logoutInteractive(
        context: context,
        title: title,
        request: request,
      );
    },
  );
}