Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop the app saving settings and progress under the wrong account after signing in

Barnabas849d59cb5711
diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dart
index e0dae7f5..1af410ea 100644
--- a/lib/providers/auth_provider.dart
+++ b/lib/providers/auth_provider.dart
@@ -812,7 +812,9 @@ class AuthProvider extends ChangeNotifier {
customHeaders: customHeaders,
),
);
- } catch (_) {}
+ } catch (e) {
+ debugPrint('[Auth] Saving the account failed: $e');
+ }
await _onAccountActivated();
@@ -903,7 +905,9 @@ class AuthProvider extends ChangeNotifier {
customHeaders: customHeaders,
),
);
- } catch (_) {}
+ } catch (e) {
+ debugPrint('[Auth] Saving the account failed: $e');
+ }
await _onAccountActivated();
@@ -1016,7 +1020,9 @@ class AuthProvider extends ChangeNotifier {
customHeaders: customHeaders,
),
);
- } catch (_) {}
+ } catch (e) {
+ debugPrint('[Auth] Saving the account failed: $e');
+ }
await _onAccountActivated();
diff --git a/lib/services/signed_out_playback.dart b/lib/services/signed_out_playback.dart
index 6ddb3573..057c1fdc 100644
--- a/lib/services/signed_out_playback.dart
+++ b/lib/services/signed_out_playback.dart
@@ -50,10 +50,14 @@ class SignedOutPlayback {
_active = false;
debugPrint('[SignedOut] Signing in, stopping signed-out playback first');
UserAccountService.beforeAccountChange = null;
- final player = AudioPlayerService();
- if (player.hasBook) {
- await player.pause();
- await player.stop();
+ try {
+ final player = AudioPlayerService();
+ if (player.hasBook) {
+ await player.pause();
+ await player.stop();
+ }
+ } catch (e) {
+ debugPrint('[SignedOut] Stopping playback failed: $e');
}
}
}
diff --git a/lib/services/user_account_service.dart b/lib/services/user_account_service.dart
index f4347af7..a068ae6f 100644
--- a/lib/services/user_account_service.dart
+++ b/lib/services/user_account_service.dart
@@ -126,11 +126,43 @@ class UserAccountService {
// Load active scope
_activeScopeKey = prefs.getString(_activeKey);
debugPrint('[UserAccount] Loaded ${_accounts.length} accounts, active=$_activeScopeKey');
+ await _alignScopeWithSession(prefs);
+ }
+
+ /// The signed-in session lives in the plain server_url / username keys
+ /// while everything per account is stored under the active scope. If the
+ /// two ever disagree, the app talks to the server as one user but saves
+ /// progress and settings under another, so trust the session and point the
+ /// scope back at it.
+ Future<void> _alignScopeWithSession(SharedPreferences prefs) async {
+ final server = prefs.getString('server_url');
+ final username = prefs.getString('username');
+ if (server == null || username == null || username.isEmpty) return;
+ SavedAccount? match;
+ for (final a in _accounts) {
+ if (a.username == username &&
+ normalizeServerUrl(a.serverUrl) == normalizeServerUrl(server)) {
+ match = a;
+ break;
+ }
+ }
+ if (match == null || match.scopeKey == _activeScopeKey) return;
+ debugPrint('[UserAccount] Active scope $_activeScopeKey did not match the '
+ 'signed-in $username - switching the scope to ${match.scopeKey}');
+ _activeScopeKey = match.scopeKey;
+ await _persistActiveKey();
}
/// Save or update an account after login. Sets it as active.
Future<void> saveAccount(SavedAccount account) async {
- await beforeAccountChange?.call();
+ // Nothing in here may stop the account being saved: a sign-in that got
+ // this far has already written the new session, and bailing out now
+ // leaves the old account's scope active under it.
+ try {
+ await beforeAccountChange?.call();
+ } catch (e) {
+ debugPrint('[UserAccount] beforeAccountChange failed: $e');
+ }
// Remove existing entry for same server+username (update token)
_accounts.removeWhere(
(a) => a.serverUrl == account.serverUrl && a.username == account.username);