Skip to content

Commit 6adfea4

Browse files
committed
Add the silent logout function
1 parent 0cdc371 commit 6adfea4

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

lib/solidpod.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ export 'src/solid/utils/misc.dart'
116116
isDir,
117117
logoutPod,
118118
registerLogoutCacheCallback,
119-
setAppDirName;
119+
setAppDirName,
120+
silentLogout;
120121

121122
/// Helper for deleting files and containers from a Solid POD
122123

lib/src/solid/utils/misc.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import 'package:flutter/foundation.dart' show debugPrint;
3434

3535
import 'package:encrypter_plus/encrypter_plus.dart';
3636
import 'package:fast_rsa/fast_rsa.dart' show KeyPair;
37+
import 'package:http/http.dart' as http;
3738
import 'package:intl/intl.dart';
3839
import 'package:jwt_decoder/jwt_decoder.dart';
3940
import 'package:path/path.dart' as path;
@@ -518,6 +519,49 @@ Future<bool> logoutPod() async {
518519
}
519520
}
520521

522+
/// Clear all login state without opening a browser.
523+
///
524+
/// Performs the same cleanup as [logoutPod] but invalidates the IdP session
525+
/// via a headless HTTP request rather than launching a visible browser
526+
/// window. Use this when switching accounts or recovering from stale
527+
/// credentials.
528+
529+
Future<bool> silentLogout() async {
530+
try {
531+
await KeyManager.clear();
532+
533+
final logoutUrl = await AuthDataManager.getLogoutUrl();
534+
final authDataRemoved = await AuthDataManager.removeAuthData();
535+
536+
if (_onLogoutClearCaches != null) {
537+
try {
538+
await _onLogoutClearCaches!();
539+
} on Object catch (e) {
540+
debugPrint('silentLogout() cache callback failed (non-critical): $e');
541+
}
542+
}
543+
544+
// Best-effort IdP session invalidation via headless HTTP GET.
545+
546+
if (logoutUrl != null && logoutUrl.isNotEmpty) {
547+
try {
548+
await http.get(Uri.parse(logoutUrl));
549+
} on Object catch (e) {
550+
debugPrint('silentLogout() headless logout failed (non-critical): $e');
551+
}
552+
}
553+
554+
return authDataRemoved;
555+
} on Object catch (e) {
556+
debugPrint('silentLogout() CRITICAL: $e');
557+
try {
558+
await AuthDataManager.removeAuthData();
559+
await KeyManager.clear();
560+
} on Object catch (_) {}
561+
return false;
562+
}
563+
}
564+
521565
/// Removes header and footer (which mess up the TTL format) from a PEM-formatted public key string.
522566
///
523567
/// This function takes a public key string, typically in PEM format, and removes

0 commit comments

Comments
 (0)