logoutInteractive method
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,
);
},
);
}