Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix cross-account data leak and stop playback on account switch

Nathan Poulson42e1218cb41f
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index 969c541d..e59faedb 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -1904,6 +1904,16 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
}
+ /// Stop any active playback and sync progress to the server before
+ /// switching users, adding an account, or signing out.
+ Future<void> _stopAndSyncPlayback() async {
+ final player = AudioPlayerService();
+ if (player.hasBook) {
+ await player.pause();
+ await player.stop();
+ }
+ }
+
void _confirmLogout(BuildContext context) {
final cs = Theme.of(context).colorScheme;
showDialog(
@@ -1918,9 +1928,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
child: const Text('Stay'),
),
FilledButton(
- onPressed: () {
+ onPressed: () async {
Navigator.pop(ctx);
- context.read<AuthProvider>().logout();
+ await _stopAndSyncPlayback();
+ if (context.mounted) context.read<AuthProvider>().logout();
},
style: FilledButton.styleFrom(backgroundColor: cs.error),
child: const Text('Sign Out'),
@@ -1931,6 +1942,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
void _addAccount(BuildContext context) async {
+ // Stop playback and sync before navigating to login
+ await _stopAndSyncPlayback();
+ if (!context.mounted) return;
// Navigate to login screen as a pushed route (not replacing current)
await Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(builder: (_) => const LoginScreen()),
@@ -1991,6 +2005,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
if (confirmed != true || !context.mounted) return;
+ // Stop playback and sync before switching
+ await _stopAndSyncPlayback();
+ if (!context.mounted) return;
+
final auth = context.read<AuthProvider>();
final lib = context.read<LibraryProvider>();
diff --git a/lib/services/scoped_prefs.dart b/lib/services/scoped_prefs.dart
index b98282b0..bbf4f3ca 100644
--- a/lib/services/scoped_prefs.dart
+++ b/lib/services/scoped_prefs.dart
@@ -12,16 +12,19 @@ class ScopedPrefs {
static String _scope(String key) => UserAccountService().scopedKey(key);
+ /// Only fall back to un-scoped data when there is no active scope
+ /// (i.e. pre-multi-user migration). Once scoped, a missing key means
+ /// the account has no data — not that it should inherit another account's.
+ static bool get _shouldFallback => !UserAccountService().hasScope;
+
// ── String ──
static Future<String?> getString(String key) async {
final prefs = await SharedPreferences.getInstance();
- // Try scoped key first, fall back to un-scoped (migration)
final scoped = prefs.getString(_scope(key));
if (scoped != null) return scoped;
- // Fallback: if no scoped value but un-scoped exists, return it
- // (this handles pre-multi-user data transparently)
- return prefs.getString(key);
+ if (_shouldFallback) return prefs.getString(key);
+ return null;
}
static Future<void> setString(String key, String value) async {
@@ -40,7 +43,8 @@ class ScopedPrefs {
final prefs = await SharedPreferences.getInstance();
final scoped = prefs.getStringList(_scope(key));
if (scoped != null) return scoped;
- return prefs.getStringList(key) ?? [];
+ if (_shouldFallback) return prefs.getStringList(key) ?? [];
+ return [];
}
static Future<void> setStringList(String key, List<String> value) async {
@@ -54,7 +58,7 @@ class ScopedPrefs {
final prefs = await SharedPreferences.getInstance();
final scoped = _scope(key);
if (prefs.containsKey(scoped)) return prefs.getBool(scoped);
- if (prefs.containsKey(key)) return prefs.getBool(key);
+ if (_shouldFallback && prefs.containsKey(key)) return prefs.getBool(key);
return null;
}
@@ -69,7 +73,7 @@ class ScopedPrefs {
final prefs = await SharedPreferences.getInstance();
final scoped = _scope(key);
if (prefs.containsKey(scoped)) return prefs.getDouble(scoped);
- if (prefs.containsKey(key)) return prefs.getDouble(key);
+ if (_shouldFallback && prefs.containsKey(key)) return prefs.getDouble(key);
return null;
}
@@ -84,7 +88,7 @@ class ScopedPrefs {
final prefs = await SharedPreferences.getInstance();
final scoped = _scope(key);
if (prefs.containsKey(scoped)) return prefs.getInt(scoped);
- if (prefs.containsKey(key)) return prefs.getInt(key);
+ if (_shouldFallback && prefs.containsKey(key)) return prefs.getInt(key);
return null;
}
@@ -97,6 +101,8 @@ class ScopedPrefs {
static Future<bool> containsKey(String key) async {
final prefs = await SharedPreferences.getInstance();
- return prefs.containsKey(_scope(key)) || prefs.containsKey(key);
+ if (prefs.containsKey(_scope(key))) return true;
+ if (_shouldFallback) return prefs.containsKey(key);
+ return false;
}
}