A cross platform Audiobookshelf client for Android and iOS
Dart
Keep the session alive when the widget or a background check refreshes the login token
Barnabas39934a6be844
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dartindex 024bc84d..5b36ce88 100644--- a/lib/services/home_widget_service.dart+++ b/lib/services/home_widget_service.dart@@ -13,6 +13,7 @@ import 'audio_player_service.dart';import 'api_service.dart';import 'download_service.dart';import 'scoped_prefs.dart';+import 'user_account_service.dart';import 'wear_player_service.dart';const String _androidWidgetName = 'NowPlayingWidget';@@ -830,6 +831,8 @@ class HomeWidgetService {refreshToken: refreshToken,isLegacyToken: refreshToken == null,customHeaders: headers,+ onTokensRefreshed: (access, refresh) =>+ UserAccountService().persistRefreshedTokens(access, refresh),);}diff --git a/lib/services/user_account_service.dart b/lib/services/user_account_service.dartindex 341bf33b..b0a32c66 100644--- a/lib/services/user_account_service.dart+++ b/lib/services/user_account_service.dart@@ -156,6 +156,23 @@ class UserAccountService {}}+ /// Persist tokens refreshed outside the live session (background isolates,+ /// widget cold paths). The server rotates refresh tokens - each one is+ /// single-use - so a refresh whose result is only kept in memory strands+ /// the on-disk session with an expired access token and a consumed refresh+ /// token, and the next app launch can't sign in.+ Future<void> persistRefreshedTokens(String accessToken, String? refreshToken) async {+ final prefs = await SharedPreferences.getInstance();+ await prefs.setString('token', accessToken);+ if (refreshToken != null) await prefs.setString('refresh_token', refreshToken);+ final serverUrl = prefs.getString('server_url');+ final username = prefs.getString('username');+ if (serverUrl != null && username != null) {+ if (_accounts.isEmpty) await init();+ await updateTokens(serverUrl, username, accessToken, refreshToken: refreshToken);+ }+ }+/// Change the server URL of a saved account (e.g. a dynamic-DNS hostname/// changed) WITHOUT losing the account's per-user data. The scopeKey is/// derived from the URL, so every scoped SharedPreferences key is migrated