A cross platform Audiobookshelf client for Android and iOS
Dart
JWT auth with refresh tokens, PlayStore flavor without MANAGE_EXTERNAL_STORAGE
Nathan Poulson229001444fdb
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.ktsindex 7fc6c49d..16467e79 100644--- a/android/app/build.gradle.kts+++ b/android/app/build.gradle.kts@@ -45,6 +45,16 @@ android {versionName = flutter.versionName}+ flavorDimensions += "distribution"+ productFlavors {+ create("github") {+ dimension = "distribution"+ }+ create("playstore") {+ dimension = "distribution"+ }+ }+buildTypes {debug {signingConfig = signingConfigs.getByName("release")diff --git a/android/app/src/playstore/AndroidManifest.xml b/android/app/src/playstore/AndroidManifest.xmlnew file mode 100644index 00000000..fefb445f--- /dev/null+++ b/android/app/src/playstore/AndroidManifest.xml@@ -0,0 +1,7 @@+<manifest xmlns:android="http://schemas.android.com/apk/res/android"+ xmlns:tools="http://schemas.android.com/tools">++ <!-- Remove MANAGE_EXTERNAL_STORAGE for Play Store compliance -->+ <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:node="remove"/>++</manifest>diff --git a/build_bundle.sh b/build_bundle.shindex 718ce3f4..629c7e37 100644--- a/build_bundle.sh+++ b/build_bundle.sh@@ -1,2 +1,2 @@#!/bin/bash-flutter build appbundle --release "$@" && explorer "build\\app\\outputs\\bundle\\release"+flutter build appbundle --release --flavor playstore --dart-define=PLAYSTORE_BUILD=true "$@" && explorer "build\\app\\outputs\\bundle\\playstoreRelease"diff --git a/build_github.sh b/build_github.shindex e554b379..b83b3e04 100644--- a/build_github.sh+++ b/build_github.sh@@ -1,2 +1,2 @@#!/bin/bash-flutter build apk --release --dart-define=GITHUB_BUILD=true "$@" && cd build/app/outputs/apk/release+flutter build apk --release --flavor github --dart-define=GITHUB_BUILD=true "$@" && explorer "build\\app\\outputs\\apk\\github\\release"diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dartindex 2944f260..1a58681b 100644--- a/lib/providers/auth_provider.dart+++ b/lib/providers/auth_provider.dart@@ -10,7 +10,9 @@ import '../services/user_account_service.dart';import '../main.dart' show scaffoldMessengerKey;class AuthProvider extends ChangeNotifier {- String? _token;+ String? _accessToken;+ String? _refreshToken;+ bool _isLegacyToken = false;String? _serverUrl;String? _username;String? _userId;@@ -30,10 +32,11 @@ class AuthProvider extends ChangeNotifier {String? _errorMessage;// Getters- bool get isAuthenticated => _token != null && _serverUrl != null;+ bool get isAuthenticated => _accessToken != null && _serverUrl != null;bool get isLoading => _isLoading;bool get serverReachable => _serverReachable;- String? get token => _token;+ /// Current access token (or legacy token for old servers).+ String? get token => _accessToken;String? get serverUrl => _serverUrl;String? get activeServerUrl => (_useLocalServer && _localServerUrl.isNotEmpty) ? _localServerUrl : _serverUrl;bool get useLocalServer => _useLocalServer;@@ -56,12 +59,46 @@ class AuthProvider extends ChangeNotifier {ApiService? get apiService {final url = activeServerUrl;- if (url != null && _token != null) {- return ApiService(baseUrl: url, token: _token!, customHeaders: _customHeaders);+ if (url != null && _accessToken != null) {+ return ApiService(+ baseUrl: url,+ token: _accessToken!,+ refreshToken: _refreshToken,+ isLegacyToken: _isLegacyToken,+ customHeaders: _customHeaders,+ onTokensRefreshed: _onTokensRefreshed,+ onAuthExpired: _onAuthExpired,+ );}return null;}+ void _onTokensRefreshed(String newAccessToken, String? newRefreshToken) {+ _accessToken = newAccessToken;+ if (newRefreshToken != null) _refreshToken = newRefreshToken;+ // Persist updated tokens+ SharedPreferences.getInstance().then((prefs) {+ prefs.setString('token', _accessToken!);+ if (_refreshToken != null) prefs.setString('refresh_token', _refreshToken!);+ });+ // Update saved account+ if (_serverUrl != null && _username != null) {+ UserAccountService().updateTokens(_serverUrl!, _username!, _accessToken!, refreshToken: _refreshToken);+ }+ // Push new token to socket+ SocketService().updateToken(_accessToken!);+ notifyListeners();+ }++ void _onAuthExpired() {+ debugPrint('[Auth] Token refresh failed, forcing re-login');+ // Show a message to the user+ scaffoldMessengerKey.currentState?.showSnackBar(+ const SnackBar(content: Text('Session expired. Please log in again.')),+ );+ logout();+ }+/// Try to restore a saved session from SharedPreferences./// If the server is unreachable, still restore credentials so offline mode works.Future<void> tryRestoreSession() async {@@ -77,15 +114,18 @@ class AuthProvider extends ChangeNotifier {debugPrint('[Auth] SharedPreferences loaded (${sw.elapsedMilliseconds}ms)');final savedUrl = prefs.getString('server_url');final savedToken = prefs.getString('token');+ final savedRefreshToken = prefs.getString('refresh_token');final savedUsername = prefs.getString('username');final savedLibraryId = prefs.getString('default_library_id');- debugPrint('[Auth] saved credentials: url=${savedUrl != null}, token=${savedToken != null}');+ debugPrint('[Auth] saved credentials: url=${savedUrl != null}, token=${savedToken != null}, refreshToken=${savedRefreshToken != null}');if (savedUrl != null && savedToken != null) {// Always restore credentials so we can at least go offline_serverUrl = savedUrl;- _token = savedToken;+ _accessToken = savedToken;+ _refreshToken = savedRefreshToken;+ _isLegacyToken = savedRefreshToken == null;_username = savedUsername;_userId = prefs.getString('user_id');_defaultLibraryId = savedLibraryId;@@ -129,7 +169,15 @@ class AuthProvider extends ChangeNotifier {if (reachable) {try {debugPrint('[Auth] fetching /me... (${sw.elapsedMilliseconds}ms)');- final api = ApiService(baseUrl: activeServerUrl!, token: savedToken, customHeaders: _customHeaders);+ final api = ApiService(+ baseUrl: activeServerUrl!,+ token: savedToken,+ refreshToken: savedRefreshToken,+ isLegacyToken: _isLegacyToken,+ customHeaders: _customHeaders,+ onTokensRefreshed: _onTokensRefreshed,+ onAuthExpired: _onAuthExpired,+ );final me = await api.getMe();if (me != null) {_userJson = me;@@ -201,7 +249,12 @@ class AuthProvider extends ChangeNotifier {}_serverUrl = url;- _token = user['token'] as String?;+ // Prefer new JWT accessToken, fall back to legacy user.token for old servers+ final newAccessToken = result['accessToken'] as String?;+ final newRefreshToken = result['refreshToken'] as String?;+ _isLegacyToken = newAccessToken == null;+ _accessToken = newAccessToken ?? user['token'] as String?;+ _refreshToken = newRefreshToken;_username = user['username'] as String?;_userId = user['id'] as String?;_defaultLibraryId = result['userDefaultLibraryId'] as String?;@@ -222,13 +275,13 @@ class AuthProvider extends ChangeNotifier {try {final prefs = await SharedPreferences.getInstance();await prefs.setString('server_url', _serverUrl!);- if (_token != null) await prefs.setString('token', _token!);+ if (_accessToken != null) await prefs.setString('token', _accessToken!);+ if (_refreshToken != null) await prefs.setString('refresh_token', _refreshToken!);if (_username != null) await prefs.setString('username', _username!);if (_userId != null) await prefs.setString('user_id', _userId!);if (_defaultLibraryId != null) {await prefs.setString('default_library_id', _defaultLibraryId!);}- // Persist custom headers as JSONif (customHeaders.isNotEmpty) {await prefs.setString('custom_headers', jsonEncode(customHeaders));} else {@@ -241,8 +294,10 @@ class AuthProvider extends ChangeNotifier {await UserAccountService().saveAccount(SavedAccount(serverUrl: _serverUrl!,username: _username ?? '',- token: _token ?? '',+ token: _accessToken ?? '',+ refreshToken: _refreshToken,userId: _userId,+ isLegacyToken: _isLegacyToken,));} catch (_) {}@@ -270,7 +325,11 @@ class AuthProvider extends ChangeNotifier {}_serverUrl = url;- _token = user['token'] as String?;+ final newAccessToken = result['accessToken'] as String?;+ final newRefreshToken = result['refreshToken'] as String?;+ _isLegacyToken = newAccessToken == null;+ _accessToken = newAccessToken ?? user['token'] as String?;+ _refreshToken = newRefreshToken;_username = user['username'] as String?;_userId = user['id'] as String?;_defaultLibraryId = result['userDefaultLibraryId'] as String?;@@ -291,7 +350,8 @@ class AuthProvider extends ChangeNotifier {try {final prefs = await SharedPreferences.getInstance();await prefs.setString('server_url', _serverUrl!);- if (_token != null) await prefs.setString('token', _token!);+ if (_accessToken != null) await prefs.setString('token', _accessToken!);+ if (_refreshToken != null) await prefs.setString('refresh_token', _refreshToken!);if (_username != null) await prefs.setString('username', _username!);if (_userId != null) await prefs.setString('user_id', _userId!);if (_defaultLibraryId != null) {@@ -304,8 +364,10 @@ class AuthProvider extends ChangeNotifier {await UserAccountService().saveAccount(SavedAccount(serverUrl: _serverUrl!,username: _username ?? '',- token: _token ?? '',+ token: _accessToken ?? '',+ refreshToken: _refreshToken,userId: _userId,+ isLegacyToken: _isLegacyToken,));} catch (_) {}@@ -408,7 +470,9 @@ class AuthProvider extends ChangeNotifier {final logoutServer = _serverUrl;final logoutUser = _username;- _token = null;+ _accessToken = null;+ _refreshToken = null;+ _isLegacyToken = false;_serverUrl = null;_username = null;_userId = null;@@ -425,6 +489,7 @@ class AuthProvider extends ChangeNotifier {final prefs = await SharedPreferences.getInstance();await prefs.remove('server_url');await prefs.remove('token');+ await prefs.remove('refresh_token');await prefs.remove('username');await prefs.remove('user_id');await prefs.remove('default_library_id');@@ -454,7 +519,9 @@ class AuthProvider extends ChangeNotifier {// Set credentials_serverUrl = account.serverUrl;- _token = account.token;+ _accessToken = account.token;+ _refreshToken = account.refreshToken;+ _isLegacyToken = account.isLegacyToken;_username = account.username;_userId = account.userId;_defaultLibraryId = null;@@ -468,7 +535,7 @@ class AuthProvider extends ChangeNotifier {try {final prefs = await SharedPreferences.getInstance();await prefs.setString('server_url', _serverUrl!);- if (_token != null) await prefs.setString('token', _token!);+ if (_accessToken != null) await prefs.setString('token', _accessToken!);if (_username != null) await prefs.setString('username', _username!);if (_userId != null) await prefs.setString('user_id', _userId!);} catch (_) {}@@ -492,7 +559,15 @@ class AuthProvider extends ChangeNotifier {// Verify the token still works and get user infotry {- final api = ApiService(baseUrl: _serverUrl!, token: _token!, customHeaders: _customHeaders);+ final api = ApiService(+ baseUrl: _serverUrl!,+ token: _accessToken!,+ refreshToken: _refreshToken,+ isLegacyToken: _isLegacyToken,+ customHeaders: _customHeaders,+ onTokensRefreshed: _onTokensRefreshed,+ onAuthExpired: _onAuthExpired,+ );final me = await api.getMe();if (me != null) {_userJson = me;diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dartindex efefb050..6f442a00 100644--- a/lib/screens/settings_screen.dart+++ b/lib/screens/settings_screen.dart@@ -33,6 +33,7 @@ class SettingsScreen extends StatefulWidget {}class _SettingsScreenState extends State<SettingsScreen> {+ static const _isPlayStoreBuild = bool.fromEnvironment('PLAYSTORE_BUILD');AutoRewindSettings _rewindSettings = const AutoRewindSettings();double _defaultSpeed = 1.0;bool _wifiOnlyDownloads = false;@@ -82,6 +83,7 @@ class _SettingsScreenState extends State<SettingsScreen> {bool _trustAllCerts = false;bool _loaded = false;String _downloadLocationLabel = 'App Internal Storage (Default)';+ bool _canPickDownloadLocation = false;int _totalDownloadSizeBytes = 0;int _deviceTotalBytes = 0;int _deviceAvailableBytes = 0;@@ -273,6 +275,7 @@ class _SettingsScreenState extends State<SettingsScreen> {_skipChapterBarrier = skipBarrier;_trustAllCerts = trustCerts;_showExplicitBadge = showExplicit;+ _canPickDownloadLocation = !_isPlayStoreBuild;_loaded = true;});@@ -1518,7 +1521,7 @@ class _SettingsScreenState extends State<SettingsScreen> {} : null,),const Divider(height: 1, indent: 16, endIndent: 16),- if (!Platform.isIOS)+ if (!Platform.isIOS && _canPickDownloadLocation)ListTile(leading: Icon(Icons.folder_outlined, color: cs.primary),title: const Text('Download location'),diff --git a/lib/services/android_auto_service.dart b/lib/services/android_auto_service.dartindex 295d6e9c..98168a09 100644--- a/lib/services/android_auto_service.dart+++ b/lib/services/android_auto_service.dart@@ -199,8 +199,9 @@ class AndroidAutoService {final prefs = await SharedPreferences.getInstance();final url = prefs.getString('server_url');final token = prefs.getString('token');+ final refreshToken = prefs.getString('refresh_token');if (url == null || token == null) return null;- return ApiService(baseUrl: url, token: token);+ return ApiService(baseUrl: url, token: token, refreshToken: refreshToken, isLegacyToken: refreshToken == null);}Future<String?> getDefaultLibraryId() async {diff --git a/lib/services/api_service.dart b/lib/services/api_service.dartindex 6aef856b..ce811891 100644--- a/lib/services/api_service.dart+++ b/lib/services/api_service.dart@@ -1,3 +1,4 @@+import 'dart:async';import 'dart:convert';import 'package:flutter/foundation.dart';import 'package:http/http.dart' as http;@@ -24,10 +25,21 @@ class ApiService {}final String baseUrl;- final String token;+ String _accessToken;+ String? _refreshToken;+ bool _isLegacyToken;final Map<String, String> customHeaders;- // Device info — set once at app start+ /// Called after a successful token refresh so the auth layer can persist.+ void Function(String newAccessToken, String? newRefreshToken)? onTokensRefreshed;++ /// Called when refresh fails and the user must re-login.+ VoidCallback? onAuthExpired;++ // Concurrency lock for refresh+ Completer<bool>? _refreshCompleter;++ // Device info - set once at app startstatic String deviceManufacturer = '';static String deviceModel = '';static String deviceId = '';@@ -38,31 +50,147 @@ class ApiService {final prefs = await SharedPreferences.getInstance();var id = prefs.getString('absorb_device_id');if (id == null || id.isEmpty) {- // Generate a unique ID for this installid = 'absorb-${DateTime.now().millisecondsSinceEpoch.toRadixString(36)}-${(DateTime.now().microsecond * 31337).toRadixString(36)}';await prefs.setString('absorb_device_id', id);}deviceId = id;}- ApiService({required this.baseUrl, required this.token, this.customHeaders = const {}});+ ApiService({+ required this.baseUrl,+ required String token,+ String? refreshToken,+ bool isLegacyToken = false,+ this.customHeaders = const {},+ this.onTokensRefreshed,+ this.onAuthExpired,+ }) : _accessToken = token,+ _refreshToken = refreshToken,+ _isLegacyToken = isLegacyToken;++ /// Current access token (for external use like cover URLs, socket auth).+ String get token => _accessToken;Map<String, String> get _headers => {...customHeaders,- 'Authorization': 'Bearer $token',+ 'Authorization': 'Bearer $_accessToken','Content-Type': 'application/json',};/// Public headers for image/audio requests (no Content-Type needed).- /// Use this for CachedNetworkImage, Image.network, AudioSource.uri, etc.Map<String, String> get mediaHeaders => {...customHeaders,- 'Authorization': 'Bearer $token',+ 'Authorization': 'Bearer $_accessToken',};String get _cleanBaseUrl =>baseUrl.endsWith('/') ? baseUrl.substring(0, baseUrl.length - 1) : baseUrl;+ /// Attempt to refresh the access token using the refresh token.+ /// Returns true if successful. Uses a lock so concurrent 401s only+ /// trigger one refresh.+ Future<bool> _refreshAccessToken() async {+ if (_isLegacyToken || _refreshToken == null) return false;++ // If a refresh is already in progress, wait for it+ if (_refreshCompleter != null) return _refreshCompleter!.future;++ _refreshCompleter = Completer<bool>();+ try {+ final response = await http.post(+ Uri.parse('$_cleanBaseUrl/api/authorize'),+ headers: {+ ...customHeaders,+ 'Authorization': 'Bearer $_refreshToken',+ 'x-return-tokens': 'true',+ },+ );++ if (response.statusCode == 200) {+ final data = jsonDecode(response.body) as Map<String, dynamic>;+ final newAccess = data['accessToken'] as String?;+ final newRefresh = data['refreshToken'] as String?;+ if (newAccess != null) {+ _accessToken = newAccess;+ if (newRefresh != null) _refreshToken = newRefresh;+ onTokensRefreshed?.call(_accessToken, _refreshToken);+ debugPrint('[API] Token refreshed successfully');+ _refreshCompleter!.complete(true);+ return true;+ }+ }+ debugPrint('[API] Token refresh failed: ${response.statusCode}');+ _refreshCompleter!.complete(false);+ return false;+ } catch (e) {+ debugPrint('[API] Token refresh error: $e');+ _refreshCompleter!.complete(false);+ return false;+ } finally {+ _refreshCompleter = null;+ }+ }++ /// Make an authenticated GET request, retrying once on 401 with a refreshed token.+ Future<http.Response> _authGet(Uri url, {Map<String, String>? headers, Duration timeout = const Duration(seconds: 15)}) async {+ final h = headers ?? _headers;+ var response = await http.get(url, headers: h).timeout(timeout);+ if (response.statusCode == 401 && !_isLegacyToken) {+ if (await _refreshAccessToken()) {+ final refreshedHeaders = Map<String, String>.from(h)+ ..['Authorization'] = 'Bearer $_accessToken';+ response = await http.get(url, headers: refreshedHeaders).timeout(timeout);+ }+ if (response.statusCode == 401) onAuthExpired?.call();+ }+ return response;+ }++ /// Make an authenticated POST request, retrying once on 401 with a refreshed token.+ Future<http.Response> _authPost(Uri url, {Map<String, String>? headers, Object? body, Duration timeout = const Duration(seconds: 15)}) async {+ final h = headers ?? _headers;+ var response = await http.post(url, headers: h, body: body).timeout(timeout);+ if (response.statusCode == 401 && !_isLegacyToken) {+ if (await _refreshAccessToken()) {+ final refreshedHeaders = Map<String, String>.from(h)+ ..['Authorization'] = 'Bearer $_accessToken';+ response = await http.post(url, headers: refreshedHeaders, body: body).timeout(timeout);+ }+ if (response.statusCode == 401) onAuthExpired?.call();+ }+ return response;+ }++ /// Make an authenticated PATCH request, retrying once on 401 with a refreshed token.+ Future<http.Response> _authPatch(Uri url, {Map<String, String>? headers, Object? body, Duration timeout = const Duration(seconds: 15)}) async {+ final h = headers ?? _headers;+ var response = await http.patch(url, headers: h, body: body).timeout(timeout);+ if (response.statusCode == 401 && !_isLegacyToken) {+ if (await _refreshAccessToken()) {+ final refreshedHeaders = Map<String, String>.from(h)+ ..['Authorization'] = 'Bearer $_accessToken';+ response = await http.patch(url, headers: refreshedHeaders, body: body).timeout(timeout);+ }+ if (response.statusCode == 401) onAuthExpired?.call();+ }+ return response;+ }++ /// Make an authenticated DELETE request, retrying once on 401 with a refreshed token.+ Future<http.Response> _authDelete(Uri url, {Map<String, String>? headers, Duration timeout = const Duration(seconds: 15)}) async {+ final h = headers ?? _headers;+ var response = await http.delete(url, headers: h).timeout(timeout);+ if (response.statusCode == 401 && !_isLegacyToken) {+ if (await _refreshAccessToken()) {+ final refreshedHeaders = Map<String, String>.from(h)+ ..['Authorization'] = 'Bearer $_accessToken';+ response = await http.delete(url, headers: refreshedHeaders).timeout(timeout);+ }+ if (response.statusCode == 401) onAuthExpired?.call();+ }+ return response;+ }+/// Login and return the full response JSON (contains user, token, etc.) and HTTP status code.static Future<(Map<String, dynamic>?, int)> login({required String serverUrl,@@ -77,9 +205,9 @@ class ApiService {try {final response = await http.post(Uri.parse(url),- headers: {...customHeaders, 'Content-Type': 'application/json'},+ headers: {...customHeaders, 'Content-Type': 'application/json', 'x-return-tokens': 'true'},body: jsonEncode({'username': username, 'password': password}),- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {return (jsonDecode(response.body) as Map<String, dynamic>, 200);@@ -123,10 +251,9 @@ class ApiService {/// Get all libraries.Future<List<dynamic>> getLibraries() async {try {- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {final data = jsonDecode(response.body);@@ -160,11 +287,10 @@ class ApiService {}if (limit != null) query['limit'] = '$limit';- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/personalized').replace(queryParameters: query.isEmpty ? null : query),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {final data = jsonDecode(response.body);@@ -199,10 +325,9 @@ class ApiService {if (filter != null) url += '&filter=$filter';if (expanded) url += '&minified=0';if (collapseSeries) url += '&collapseseries=1';- final response = await http.get(+ final response = await _authGet(Uri.parse(url),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {return jsonDecode(response.body) as Map<String, dynamic>;@@ -224,10 +349,9 @@ class ApiService {/// Get current user info including all mediaProgress.Future<Map<String, dynamic>?> getMe() async {try {- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/me'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (response.statusCode == 200) {return jsonDecode(response.body) as Map<String, dynamic>;@@ -242,10 +366,9 @@ class ApiService {/// Get user's listening stats.Future<Map<String, dynamic>?> getListeningStats() async {try {- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/me/listening-stats'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (response.statusCode == 200) {return jsonDecode(response.body) as Map<String, dynamic>;@@ -257,10 +380,9 @@ class ApiService {/// Get user's listening sessions (paginated).Future<Map<String, dynamic>?> getListeningSessions({int page = 0, int itemsPerPage = 20}) async {try {- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/me/listening-sessions?itemsPerPage=$itemsPerPage&page=$page'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (response.statusCode == 200) {return jsonDecode(response.body) as Map<String, dynamic>;@@ -278,13 +400,12 @@ class ApiService {int desc = 1,}) async {try {- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/series''?page=$page&limit=$limit&sort=$sort&desc=$desc',),- headers: _headers,- ).timeout(const Duration(seconds: 30)+ timeout: const Duration(seconds: 30),);if (response.statusCode == 200) {@@ -307,10 +428,9 @@ class ApiService {/// Used by Android Auto to build browse tree without fetching full items.Future<Map<String, dynamic>?> getLibraryFilterData(String libraryId) async {try {- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId?include=filterdata'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {final data = jsonDecode(response.body) as Map<String, dynamic>;@@ -333,10 +453,9 @@ class ApiService {final filterValue = base64Encode(utf8.encode(authorId));final url = '$_cleanBaseUrl/api/libraries/$libraryId/items''?filter=authors.$filterValue&sort=media.metadata.title&limit=$limit';- final response = await http.get(+ final response = await _authGet(Uri.parse(url),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {final data = jsonDecode(response.body) as Map<String, dynamic>;@@ -351,10 +470,9 @@ class ApiService {/// Get all authors for a library.Future<List<Map<String, dynamic>>> getLibraryAuthors(String libraryId) async {try {- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/authors'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {final data = jsonDecode(response.body) as Map<String, dynamic>;@@ -372,10 +490,9 @@ class ApiService {try {var url = '$_cleanBaseUrl/api/authors/$authorId?include=items';if (libraryId != null) url += '&library=$libraryId';- final response = await http.get(+ final response = await _authGet(Uri.parse(url),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {return jsonDecode(response.body) as Map<String, dynamic>;@@ -398,10 +515,9 @@ class ApiService {final url = '$_cleanBaseUrl/api/libraries/$libraryId/items''?filter=series.$filterValue''&sort=media.metadata.series.sequence&limit=$limit&collapseseries=0';- final response = await http.get(+ final response = await _authGet(Uri.parse(url),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {final data = jsonDecode(response.body) as Map<String, dynamic>;@@ -445,11 +561,10 @@ class ApiService {],};if (startOffset != null && startOffset > 0) body['startOffset'] = startOffset;- final response = await http.post(+ final response = await _authPost(Uri.parse(url),- headers: _headers,body: jsonEncode(body),- ).timeout(const Duration(seconds: 20));+ timeout: const Duration(seconds: 20));debugPrint('[ABS] Play session response: ${response.statusCode}');if (response.statusCode == 200) {@@ -488,16 +603,15 @@ class ApiService {int timeListened = 60,}) async {try {- await http.post(+ await _authPost(Uri.parse('$_cleanBaseUrl/api/session/$sessionId/sync'),- headers: _headers,body: jsonEncode({'currentTime': currentTime,'timeListened': timeListened,'duration': duration,'progress': duration > 0 ? currentTime / duration : 0,}),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));} catch (_) {}}@@ -505,10 +619,9 @@ class ApiService {/// POST /api/session/:id/closeFuture<void> closePlaybackSession(String sessionId) async {try {- await http.post(+ await _authPost(Uri.parse('$_cleanBaseUrl/api/session/$sessionId/close'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));} catch (_) {}}@@ -521,10 +634,9 @@ class ApiService {final progressPath = itemId.length > 36? '${itemId.substring(0, 36)}/${itemId.substring(37)}': itemId;- final resp = await http.get(+ final resp = await _authGet(Uri.parse('$_cleanBaseUrl/api/me/progress/$progressPath'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -552,11 +664,10 @@ class ApiService {: itemId;debugPrint('[API] updateProgress PATCH /api/me/progress/$progressPath');debugPrint('[API] updateProgress body: currentTime=$currentTime');- final resp = await http.patch(+ final resp = await _authPatch(Uri.parse('$_cleanBaseUrl/api/me/progress/$progressPath'),- headers: _headers,body: body,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));debugPrint('[API] updateProgress response: ${resp.statusCode} ${resp.body}');} catch (e) {debugPrint('[API] updateProgress error: $e');@@ -593,10 +704,9 @@ class ApiService {final progressPath = itemId.length > 36? '${itemId.substring(0, 36)}/${itemId.substring(37)}': itemId;- final resp = await http.delete(+ final resp = await _authDelete(Uri.parse('$_cleanBaseUrl/api/me/progress/$progressPath'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));debugPrint('[API] deleteProgress response: ${resp.statusCode} ${resp.body}');return resp.statusCode >= 200 && resp.statusCode < 300;} catch (e) {@@ -616,10 +726,9 @@ class ApiService {final episodeId = isCompound ? itemId.substring(37) : null;// DELETE progress entry- await http.delete(+ await _authDelete(Uri.parse('$_cleanBaseUrl/api/me/progress/$progressPath'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));// Start session at 0 and close — forces server to update positionfinal sessionData = await startPlaybackSession(apiItemId, episodeId: episodeId);@@ -632,9 +741,8 @@ class ApiService {}// PATCH last to hide from continue listening (after session sync)- await http.patch(+ await _authPatch(Uri.parse('$_cleanBaseUrl/api/me/progress/$progressPath'),- headers: _headers,body: jsonEncode({'currentTime': 0,'progress': 0,@@ -642,7 +750,7 @@ class ApiService {'hideFromContinueListening': true,'lastUpdate': DateTime.now().millisecondsSinceEpoch,}),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));return true;} catch (e) {@@ -660,9 +768,8 @@ class ApiService {try {final url = '$_cleanBaseUrl/api/items/$itemId/play/$episodeId';debugPrint('[ABS] Starting episode session: POST $url');- final response = await http.post(+ final response = await _authPost(Uri.parse(url),- headers: _headers,body: jsonEncode({'deviceInfo': {'clientName': 'Absorb',@@ -683,7 +790,7 @@ class ApiService {'audio/aac',],}),- ).timeout(const Duration(seconds: 20));+ timeout: const Duration(seconds: 20));debugPrint('[ABS] Episode session response: ${response.statusCode}');if (response.statusCode == 200) {@@ -702,10 +809,9 @@ class ApiService {Future<Map<String, dynamic>?> getEpisodeProgress(String itemId, String episodeId) async {try {- final resp = await http.get(+ final resp = await _authGet(Uri.parse('$_cleanBaseUrl/api/me/progress/$itemId/$episodeId'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -723,16 +829,15 @@ class ApiService {bool isFinished = false,}) async {try {- await http.patch(+ await _authPatch(Uri.parse('$_cleanBaseUrl/api/me/progress/$itemId/$episodeId'),- headers: _headers,body: jsonEncode({'currentTime': currentTime,'duration': duration,'progress': duration > 0 ? currentTime / duration : 0,'isFinished': isFinished,}),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));} catch (e) {debugPrint('[API] updateEpisodeProgress error: $e');}@@ -741,10 +846,9 @@ class ApiService {/// DELETE /api/me/progress/:itemId/:episodeIdFuture<bool> deleteEpisodeProgress(String itemId, String episodeId) async {try {- final resp = await http.delete(+ final resp = await _authDelete(Uri.parse('$_cleanBaseUrl/api/me/progress/$itemId/$episodeId'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));return resp.statusCode >= 200 && resp.statusCode < 300;} catch (e) {debugPrint('[API] deleteEpisodeProgress error: $e');@@ -756,10 +860,9 @@ class ApiService {/// GET /api/libraries/:id/recent-episodesFuture<List<dynamic>> getRecentEpisodes(String libraryId, {int limit = 25}) async {try {- final resp = await http.get(+ final resp = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/recent-episodes?limit=$limit'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (resp.statusCode == 200) {final data = jsonDecode(resp.body);if (data is Map && data['episodes'] is List) return data['episodes'] as List<dynamic>;@@ -774,10 +877,9 @@ class ApiService {/// Get a single library item with full detail (expanded=1 gives chapters, tracks, etc.)Future<Map<String, dynamic>?> getLibraryItem(String itemId) async {try {- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/items/$itemId?expanded=1&include=progress'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {return jsonDecode(response.body) as Map<String, dynamic>;@@ -797,10 +899,9 @@ class ApiService {// Get series metadataif (libraryId != null) {- final metaResp = await http.get(+ final metaResp = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/series/$seriesId'),- headers: _headers,- ).timeout(const Duration(seconds: 30));+ timeout: const Duration(seconds: 30));if (metaResp.statusCode == 200) {seriesMeta = jsonDecode(metaResp.body) as Map<String, dynamic>;}@@ -816,10 +917,9 @@ class ApiService {int page = 0;while (true) {final url = '$_cleanBaseUrl/api/libraries/$libraryId/items?filter=series.$filterValue&sort=media.metadata.series.sequence&limit=$pageSize&page=$page&collapseseries=0';- final itemsResp = await http.get(+ final itemsResp = await _authGet(Uri.parse(url),- headers: _headers,- ).timeout(const Duration(seconds: 30));+ timeout: const Duration(seconds: 30));if (itemsResp.statusCode != 200) {debugPrint('[API] getSeries items page $page failed: ${itemsResp.statusCode}');break;@@ -855,12 +955,11 @@ class ApiService {}) async {try {final encoded = Uri.encodeQueryComponent(query);- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/search?q=$encoded&limit=$limit',),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {return jsonDecode(response.body) as Map<String, dynamic>;@@ -891,10 +990,9 @@ class ApiService {final uri = Uri.parse('$_cleanBaseUrl/api/search/books').replace(queryParameters: params);debugPrint('[API] searchBooks: $uri');- final response = await http.get(+ final response = await _authGet(uri,- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );debugPrint('[API] searchBooks status=${response.statusCode} bodyLen=${response.body.length}');@@ -962,14 +1060,13 @@ class ApiService {String title, String? author) async {try {// Use the ABS server's search endpoint to query Audible for the book.- final response = await http.get(+ final response = await _authGet(Uri.parse('$_cleanBaseUrl/api/search/covers?title=${Uri.encodeQueryComponent(title)}''&author=${Uri.encodeQueryComponent(author ?? '')}''&provider=audible®ion=$_region',),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (response.statusCode == 200) {final results = jsonDecode(response.body) as List<dynamic>? ?? [];@@ -994,8 +1091,8 @@ class ApiService {/// Get all users (admin only)Future<List<dynamic>> getUsers() async {try {- final r = await http.get(Uri.parse('$_cleanBaseUrl/api/users'), headers: _headers)- .timeout(const Duration(seconds: 15));+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/users'), headers: _headers)+ ;if (r.statusCode == 200) {final data = jsonDecode(r.body);if (data is List) return data;@@ -1015,8 +1112,8 @@ class ApiService {/// Get online users (admin only)Future<List<dynamic>> getOnlineUsers() async {try {- final r = await http.get(Uri.parse('$_cleanBaseUrl/api/users/online'), headers: _headers)- .timeout(const Duration(seconds: 15));+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/users/online'), headers: _headers)+ ;if (r.statusCode == 200) {final data = jsonDecode(r.body);if (data is Map && data['usersOnline'] is List) return data['usersOnline'] as List<dynamic>;@@ -1030,10 +1127,9 @@ class ApiService {/// Get all listening sessions (admin only)Future<List<dynamic>> getAllSessions({int limit = 25}) async {try {- final r = await http.get(+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/sessions?itemsPerPage=$limit'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (r.statusCode == 200) {final data = jsonDecode(r.body);return (data['sessions'] as List<dynamic>?) ?? [];@@ -1045,8 +1141,8 @@ class ApiService {/// Get all backups (admin only)Future<List<dynamic>> getBackups() async {try {- final r = await http.get(Uri.parse('$_cleanBaseUrl/api/backups'), headers: _headers)- .timeout(const Duration(seconds: 15));+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/backups'), headers: _headers)+ ;if (r.statusCode == 200) {final data = jsonDecode(r.body);return (data['backups'] as List<dynamic>?) ?? [];@@ -1058,8 +1154,7 @@ class ApiService {/// Create a backup (admin only)Future<bool> createBackup() async {try {- final r = await http.post(Uri.parse('$_cleanBaseUrl/api/backups'), headers: _headers)- .timeout(const Duration(seconds: 60));+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/backups'), timeout: const Duration(seconds: 60));return r.statusCode == 200;} catch (e) { debugPrint('createBackup error: $e'); }return false;@@ -1068,10 +1163,9 @@ class ApiService {/// Scan a library's folders (admin only)Future<bool> scanLibrary(String libraryId) async {try {- final r = await http.post(+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/scan'),- headers: _headers,- ).timeout(const Duration(seconds: 30));+ timeout: const Duration(seconds: 30));return r.statusCode == 200;} catch (e) { debugPrint('scanLibrary error: $e'); }return false;@@ -1080,10 +1174,9 @@ class ApiService {/// Match all items in a library (admin only)Future<bool> matchLibrary(String libraryId) async {try {- final r = await http.post(+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/match'),- headers: _headers,- ).timeout(const Duration(seconds: 30));+ timeout: const Duration(seconds: 30));return r.statusCode == 200;} catch (e) { debugPrint('matchLibrary error: $e'); }return false;@@ -1092,10 +1185,9 @@ class ApiService {/// Get library stats (admin only)Future<Map<String, dynamic>?> getLibraryStats(String libraryId) async {try {- final r = await http.get(+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/stats'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (r.statusCode == 200) return jsonDecode(r.body) as Map<String, dynamic>;} catch (e) { debugPrint('getLibraryStats error: $e'); }return null;@@ -1104,8 +1196,7 @@ class ApiService {/// Purge server cache (admin only)Future<bool> purgeCache() async {try {- final r = await http.post(Uri.parse('$_cleanBaseUrl/api/cache/purge'), headers: _headers)- .timeout(const Duration(seconds: 30));+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/cache/purge'), timeout: const Duration(seconds: 30));return r.statusCode == 200;} catch (e) { debugPrint('purgeCache error: $e'); }return false;@@ -1129,11 +1220,10 @@ class ApiService {};if (permissions != null) body['permissions'] = permissions;if (librariesAccessible != null) body['librariesAccessible'] = librariesAccessible;- final r = await http.post(+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/users'),- headers: _headers,body: jsonEncode(body),- ).timeout(const Duration(seconds: 15));+ );if (r.statusCode == 200) return jsonDecode(r.body) as Map<String, dynamic>;} catch (e) { debugPrint('createUser error: $e'); }return null;@@ -1142,10 +1232,9 @@ class ApiService {/// Get a single user with full details including mediaProgress (admin only)Future<Map<String, dynamic>?> getUser(String userId) async {try {- final r = await http.get(+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/users/$userId'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (r.statusCode == 200) return jsonDecode(r.body) as Map<String, dynamic>;} catch (e) { debugPrint('getUser error: $e'); }return null;@@ -1154,11 +1243,10 @@ class ApiService {/// Update a user (admin only)Future<bool> updateUser(String userId, Map<String, dynamic> updates) async {try {- final r = await http.patch(+ final r = await _authPatch(Uri.parse('$_cleanBaseUrl/api/users/$userId'),- headers: _headers,body: jsonEncode(updates),- ).timeout(const Duration(seconds: 15));+ );return r.statusCode == 200;} catch (e) { debugPrint('updateUser error: $e'); }return false;@@ -1167,10 +1255,9 @@ class ApiService {/// Delete a user (admin only)Future<bool> deleteUser(String userId) async {try {- final r = await http.delete(+ final r = await _authDelete(Uri.parse('$_cleanBaseUrl/api/users/$userId'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );return r.statusCode == 200;} catch (e) { debugPrint('deleteUser error: $e'); }return false;@@ -1180,11 +1267,10 @@ class ApiService {/// Uses POST /api/items/:id/match which requires update permission.Future<bool> updateItemMedia(String itemId, Map<String, dynamic> media) async {try {- final r = await http.patch(+ final r = await _authPatch(Uri.parse('$_cleanBaseUrl/api/items/$itemId/media'),- headers: _headers,body: jsonEncode({'metadata': media}),- ).timeout(const Duration(seconds: 15));+ );debugPrint('[API] updateItemMedia $itemId -> ${r.statusCode}: ${r.body}');return r.statusCode == 200;} catch (e) { debugPrint('updateItemMedia error: $e'); }@@ -1194,11 +1280,10 @@ class ApiService {/// Upload a cover image URL for a library item (admin only)Future<bool> updateItemCoverUrl(String itemId, String url) async {try {- final r = await http.post(+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/items/$itemId/cover'),- headers: _headers,body: jsonEncode({'url': url}),- ).timeout(const Duration(seconds: 30));+ timeout: const Duration(seconds: 30));debugPrint('[API] updateItemCoverUrl $itemId -> ${r.statusCode}: ${r.body}');return r.statusCode == 200;} catch (e) { debugPrint('updateItemCoverUrl error: $e'); }@@ -1225,10 +1310,9 @@ class ApiService {/// Search for podcasts (uses iTunes)Future<List<dynamic>> searchPodcasts(String query) async {try {- final r = await http.get(+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/search/podcast?term=${Uri.encodeComponent(query)}'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (r.statusCode == 200) {final data = jsonDecode(r.body);if (data is List) return data;@@ -1298,11 +1382,10 @@ class ApiService {},};final bodyJson = jsonEncode(body);- final r = await http.post(+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/podcasts'),- headers: _headers,body: bodyJson,- ).timeout(const Duration(seconds: 30));+ timeout: const Duration(seconds: 30));if (r.statusCode == 200) return jsonDecode(r.body) as Map<String, dynamic>;} catch (e) { debugPrint('createPodcast error: $e'); }return null;@@ -1312,11 +1395,10 @@ class ApiService {/// POST /api/podcasts/feed body: { "rssFeed": "https://..." }Future<Map<String, dynamic>?> getPodcastFeed(String rssFeedUrl) async {try {- final r = await http.post(+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/podcasts/feed'),- headers: _headers,body: jsonEncode({'rssFeed': rssFeedUrl}),- ).timeout(const Duration(seconds: 20));+ timeout: const Duration(seconds: 20));if (r.statusCode == 200) {final data = jsonDecode(r.body);if (data is Map<String, dynamic>) return data;@@ -1328,10 +1410,9 @@ class ApiService {/// Get podcast episode download queue for a libraryFuture<Map<String, dynamic>?> getEpisodeDownloads(String libraryId) async {try {- final r = await http.get(+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/episode-downloads'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (r.statusCode == 200) return jsonDecode(r.body) as Map<String, dynamic>;} catch (e) { debugPrint('getEpisodeDownloads error: $e'); }return null;@@ -1340,10 +1421,11 @@ class ApiService {/// Download specific podcast episodesFuture<bool> downloadPodcastEpisodes(String libraryItemId, List<Map<String, dynamic>> episodes) async {try {- final r = await http.post(+ final r = await _authPost(Uri.parse('$_cleanBaseUrl/api/podcasts/$libraryItemId/download-episodes'),- headers: _headers, body: jsonEncode(episodes),- ).timeout(const Duration(seconds: 30));+ body: jsonEncode(episodes),+ timeout: const Duration(seconds: 30),+ );return r.statusCode == 200;} catch (e) { debugPrint('downloadPodcastEpisodes error: $e'); }return false;@@ -1365,10 +1447,9 @@ class ApiService {final id = libItem is Map ? libItem['id'] as String? : null;if (id == null) continue;try {- final r = await http.get(+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/podcasts/$id/checknew'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (r.statusCode == 200) success++;} catch (_) {}}@@ -1381,10 +1462,9 @@ class ApiService {/// GET /api/podcasts/:id/checknewFuture<bool> checkNewPodcastEpisodes(String podcastId) async {try {- final r = await http.get(+ final r = await _authGet(Uri.parse('$_cleanBaseUrl/api/podcasts/$podcastId/checknew'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );return r.statusCode == 200;} catch (e) { debugPrint('checkNewPodcastEpisodes error: $e'); }return false;@@ -1394,11 +1474,10 @@ class ApiService {/// PATCH /api/items/:id/media body: mediaUpdates at the media levelFuture<bool> updatePodcastMedia(String itemId, Map<String, dynamic> mediaUpdates) async {try {- final r = await http.patch(+ final r = await _authPatch(Uri.parse('$_cleanBaseUrl/api/items/$itemId/media'),- headers: _headers,body: jsonEncode(mediaUpdates),- ).timeout(const Duration(seconds: 15));+ );return r.statusCode == 200;} catch (e) { debugPrint('updatePodcastMedia error: $e'); }return false;@@ -1407,10 +1486,9 @@ class ApiService {/// Delete a podcast episodeFuture<bool> deletePodcastEpisode(String podcastId, String episodeId) async {try {- final r = await http.delete(+ final r = await _authDelete(Uri.parse('$_cleanBaseUrl/api/podcasts/$podcastId/episode/$episodeId'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );return r.statusCode == 200;} catch (e) { debugPrint('deletePodcastEpisode error: $e'); }return false;@@ -1419,10 +1497,9 @@ class ApiService {/// Delete a library item (e.g. remove a podcast show)Future<bool> deleteLibraryItem(String itemId) async {try {- final r = await http.delete(+ final r = await _authDelete(Uri.parse('$_cleanBaseUrl/api/items/$itemId'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );return r.statusCode == 200;} catch (e) { debugPrint('deleteLibraryItem error: $e'); }return false;@@ -1433,10 +1510,9 @@ class ApiService {/// GET /api/libraries/:libraryId/playlistsFuture<List<dynamic>> getLibraryPlaylists(String libraryId) async {try {- final resp = await http.get(+ final resp = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/playlists'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (resp.statusCode == 200) {final data = jsonDecode(resp.body);return (data['results'] as List<dynamic>?) ?? [];@@ -1448,10 +1524,9 @@ class ApiService {/// GET /api/playlists/:idFuture<Map<String, dynamic>?> getPlaylist(String playlistId) async {try {- final resp = await http.get(+ final resp = await _authGet(Uri.parse('$_cleanBaseUrl/api/playlists/$playlistId'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1466,15 +1541,14 @@ class ApiService {List<Map<String, dynamic>> items = const [],}) async {try {- final resp = await http.post(+ final resp = await _authPost(Uri.parse('$_cleanBaseUrl/api/playlists'),- headers: _headers,body: jsonEncode({'libraryId': libraryId,'name': name,'items': items,}),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1492,11 +1566,10 @@ class ApiService {final body = <String, dynamic>{};if (name != null) body['name'] = name;if (items != null) body['items'] = items;- final resp = await http.patch(+ final resp = await _authPatch(Uri.parse('$_cleanBaseUrl/api/playlists/$playlistId'),- headers: _headers,body: jsonEncode(body),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1507,10 +1580,9 @@ class ApiService {/// DELETE /api/playlists/:idFuture<bool> deletePlaylist(String playlistId) async {try {- final resp = await http.delete(+ final resp = await _authDelete(Uri.parse('$_cleanBaseUrl/api/playlists/$playlistId'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));return resp.statusCode == 200;} catch (_) {}return false;@@ -1525,11 +1597,10 @@ class ApiService {try {final body = <String, dynamic>{'libraryItemId': libraryItemId};if (episodeId != null) body['episodeId'] = episodeId;- final resp = await http.post(+ final resp = await _authPost(Uri.parse('$_cleanBaseUrl/api/playlists/$playlistId/item'),- headers: _headers,body: jsonEncode(body),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1546,10 +1617,9 @@ class ApiService {try {var path = '$_cleanBaseUrl/api/playlists/$playlistId/item/$libraryItemId';if (episodeId != null) path += '/$episodeId';- final resp = await http.delete(+ final resp = await _authDelete(Uri.parse(path),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1562,10 +1632,9 @@ class ApiService {/// GET /api/libraries/:libraryId/collectionsFuture<List<dynamic>> getLibraryCollections(String libraryId) async {try {- final resp = await http.get(+ final resp = await _authGet(Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/collections'),- headers: _headers,- ).timeout(const Duration(seconds: 15));+ );if (resp.statusCode == 200) {final data = jsonDecode(resp.body);return (data['results'] as List<dynamic>?) ?? [];@@ -1577,10 +1646,9 @@ class ApiService {/// GET /api/collections/:idFuture<Map<String, dynamic>?> getCollection(String collectionId) async {try {- final resp = await http.get(+ final resp = await _authGet(Uri.parse('$_cleanBaseUrl/api/collections/$collectionId'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1602,11 +1670,10 @@ class ApiService {'books': books,};if (description != null) body['description'] = description;- final resp = await http.post(+ final resp = await _authPost(Uri.parse('$_cleanBaseUrl/api/collections'),- headers: _headers,body: jsonEncode(body),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1626,11 +1693,10 @@ class ApiService {if (name != null) body['name'] = name;if (description != null) body['description'] = description;if (books != null) body['books'] = books;- final resp = await http.patch(+ final resp = await _authPatch(Uri.parse('$_cleanBaseUrl/api/collections/$collectionId'),- headers: _headers,body: jsonEncode(body),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1641,10 +1707,9 @@ class ApiService {/// DELETE /api/collections/:idFuture<bool> deleteCollection(String collectionId) async {try {- final resp = await http.delete(+ final resp = await _authDelete(Uri.parse('$_cleanBaseUrl/api/collections/$collectionId'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));return resp.statusCode == 200;} catch (_) {}return false;@@ -1656,11 +1721,10 @@ class ApiService {String libraryItemId,) async {try {- final resp = await http.post(+ final resp = await _authPost(Uri.parse('$_cleanBaseUrl/api/collections/$collectionId/book'),- headers: _headers,body: jsonEncode({'id': libraryItemId}),- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}@@ -1674,10 +1738,9 @@ class ApiService {String libraryItemId,) async {try {- final resp = await http.delete(+ final resp = await _authDelete(Uri.parse('$_cleanBaseUrl/api/collections/$collectionId/book/$libraryItemId'),- headers: _headers,- ).timeout(const Duration(seconds: 10));+ timeout: const Duration(seconds: 10));if (resp.statusCode == 200) {return jsonDecode(resp.body) as Map<String, dynamic>;}diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dartindex 1b83e219..fb642ac7 100644--- a/lib/services/home_widget_service.dart+++ b/lib/services/home_widget_service.dart@@ -79,6 +79,7 @@ class HomeWidgetService {final serverUrl = prefs.getString('server_url');final token = prefs.getString('token');+ final refreshToken = prefs.getString('refresh_token');debugPrint('[HomeWidget] play_pause: server=${serverUrl != null}, token=${token != null}');if (serverUrl == null || token == null) return;@@ -94,6 +95,8 @@ class HomeWidgetService {final api = ApiService(baseUrl: serverUrl,token: token,+ refreshToken: refreshToken,+ isLegacyToken: refreshToken == null,customHeaders: customHeaders ?? const {},);diff --git a/lib/services/log_service.dart b/lib/services/log_service.dartindex 653abe47..b2ba5999 100644--- a/lib/services/log_service.dart+++ b/lib/services/log_service.dart@@ -11,7 +11,7 @@ class LogService {factory LogService() => _instance;LogService._();- static const supportEmail = '[email protected]';+ static const supportEmail = '[email protected]';// Keep 1MB max, trim to 512KB.static const _maxSize = 1 * 1024 * 1024; // 1 MBdiff --git a/lib/services/oidc_service.dart b/lib/services/oidc_service.dartindex f72754a3..84d26ccb 100644--- a/lib/services/oidc_service.dart+++ b/lib/services/oidc_service.dart@@ -168,6 +168,7 @@ class OidcService {try {final request = await client.getUrl(Uri.parse(callbackUrl));request.followRedirects = false;+ request.headers.set('x-return-tokens', 'true');if (_rawCookies.isNotEmpty) {request.headers.set(HttpHeaders.cookieHeader, _cookieHeader);diff --git a/lib/services/socket_service.dart b/lib/services/socket_service.dartindex 36d0b585..ec560cae 100644--- a/lib/services/socket_service.dart+++ b/lib/services/socket_service.dart@@ -45,6 +45,15 @@ class SocketService {/// Called when socket.io exhausts all reconnection attempts.VoidCallback? onReconnectFailed;+ /// Update the stored token (e.g. after a JWT refresh) and re-auth if connected.+ void updateToken(String newToken) {+ _token = newToken;+ if (_socket?.connected == true) {+ debugPrint('[Socket] Re-authenticating with refreshed token');+ _socket!.emit('auth', _token);+ }+ }+void connect(String serverUrl, String token) {if (_socket != null) disconnect();diff --git a/lib/services/user_account_service.dart b/lib/services/user_account_service.dartindex 31fe6a5e..5107d3ff 100644--- a/lib/services/user_account_service.dart+++ b/lib/services/user_account_service.dart@@ -6,14 +6,18 @@ import 'package:shared_preferences/shared_preferences.dart';class SavedAccount {final String serverUrl;final String username;- final String token;+ final String token; // accessToken (or legacy token for old servers)+ final String? refreshToken;final String? userId;+ final bool isLegacyToken;SavedAccount({required this.serverUrl,required this.username,required this.token,+ this.refreshToken,this.userId,+ this.isLegacyToken = false,});/// Unique key for scoping per-user SharedPreferences data.@@ -30,14 +34,18 @@ class SavedAccount {'serverUrl': serverUrl,'username': username,'token': token,+ if (refreshToken != null) 'refreshToken': refreshToken,'userId': userId,+ if (isLegacyToken) 'isLegacyToken': true,};factory SavedAccount.fromJson(Map<String, dynamic> json) => SavedAccount(serverUrl: json['serverUrl'] as String,username: json['username'] as String,token: json['token'] as String,+ refreshToken: json['refreshToken'] as String?,userId: json['userId'] as String?,+ isLegacyToken: json['isLegacyToken'] as bool? ?? (json['refreshToken'] == null),);@override@@ -130,8 +138,8 @@ class UserAccountService {return false;}- /// Update the token for the active account (e.g. after token refresh).- Future<void> updateToken(String serverUrl, String username, String newToken) async {+ /// Update tokens for the active account (e.g. after token refresh).+ Future<void> updateTokens(String serverUrl, String username, String newToken, {String? refreshToken}) async {final idx = _accounts.indexWhere((a) => a.serverUrl == serverUrl && a.username == username);if (idx >= 0) {@@ -140,7 +148,9 @@ class UserAccountService {serverUrl: old.serverUrl,username: old.username,token: newToken,+ refreshToken: refreshToken ?? old.refreshToken,userId: old.userId,+ isLegacyToken: old.isLegacyToken,);await _persist();}diff --git a/pubspec.yaml b/pubspec.yamlindex 80a987f2..15cd865c 100644--- a/pubspec.yaml+++ b/pubspec.yaml@@ -1,7 +1,11 @@name: absorbdescription: An Audiobookshelf Clientpublish_to: 'none'-version: 1.8.40+62+version: 1.8.41+63+# v1.8.41: JWT auth (accessToken + refreshToken), MANAGE_EXTERNAL_STORAGE removed from playstore flavor+# Build scripts (run from Git Bash):+# . ./build_bundle.sh - Play Store AAB (no MANAGE_EXTERNAL_STORAGE)+# . ./build_github.sh - GitHub APK (full permissions, includes update checker)environment:sdk: '>=3.1.0 <4.0.0'