Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Show the admin settings after the app reconnects when it started up offline

Barnabas3b33a9baf98f
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index 1bd5929a..9daa4341 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -229,6 +229,11 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
_stopServerPingTimer();
_startHealthCheckTimer();
PaintingBinding.instance.imageCache.clear();
+ // If we launched offline (e.g. right after an app update), the user info
+ // (type/permissions) never loaded and admin-only UI stayed hidden. Now
+ // that we're back online, pull it so the admin settings appear without a
+ // force-close. No-op if it's already loaded.
+ unawaited(_auth?.ensureUserInfoLoaded() ?? Future.value());
if (_api != null) {
debugPrint('[Library] Back online — flushing pending syncs');
ProgressSyncService().flushPendingSync(api: _api!);
@@ -956,6 +961,10 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
setNetworkOffline(true);
} else {
notifyServerReachable(reachableUrl);
+ // Safety net: if user info never loaded (e.g. /me failed at launch
+ // while the server was technically reachable), admin-only UI stays
+ // hidden. Idempotent once loaded.
+ unawaited(_auth?.ensureUserInfoLoaded() ?? Future.value());
}
});
}
diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dart
index d2bb9281..5f361951 100644
--- a/lib/providers/auth_provider.dart
+++ b/lib/providers/auth_provider.dart
@@ -1,3 +1,4 @@
+import 'dart:async';
import 'dart:convert';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
@@ -41,6 +42,11 @@ class AuthProvider extends ChangeNotifier {
// waiting on a network timeout. Lets downloaded books start playing
// instantly when we already know we're offline.
AudioPlayerService().setKnownOffline(!value);
+ // If we launched while the server was briefly unreachable (e.g. right
+ // after an app update, before networking settles), _userJson never loaded
+ // and isAdmin stayed false — hiding the admin settings until a force-close.
+ // Now that the server is back, pull the user info we missed.
+ if (value) unawaited(_ensureUserInfo());
}
Map<String, String> _customHeaders = {};
@@ -359,6 +365,67 @@ class AuthProvider extends ChangeNotifier {
notifyListeners();
}
+ bool _ensuringUserInfo = false;
+
+ /// Public entry point for the reconnect paths (e.g. LibraryProvider going
+ /// back online for a remote server, which doesn't flow through the
+ /// `_serverReachable` setter). No-op once the user info is already loaded.
+ Future<void> ensureUserInfoLoaded() => _ensureUserInfo();
+
+ /// Fetch the current user's full info (type, permissions, ereader devices)
+ /// when we never managed to load it — e.g. the app launched while the server
+ /// was momentarily unreachable, so `tryRestoreSession` skipped the /me fetch
+ /// and `isAdmin` is stuck false. Called when the server becomes reachable
+ /// again so the admin settings appear without a force-close. No-op once we
+ /// already have the user.
+ Future<void> _ensureUserInfo() async {
+ if (_userJson != null || _ensuringUserInfo) return;
+ if (!isAuthenticated || _accessToken == null || activeServerUrl == null) return;
+ _ensuringUserInfo = true;
+ try {
+ final api = ApiService(
+ baseUrl: activeServerUrl!,
+ token: _accessToken!,
+ refreshToken: _refreshToken,
+ isLegacyToken: _isLegacyToken,
+ customHeaders: _customHeaders,
+ onTokensRefreshed: _onTokensRefreshed,
+ onAuthExpired: _onAuthExpired,
+ );
+ final auth = await api.authorize();
+ if (auth != null) {
+ final user = auth['user'] as Map<String, dynamic>?;
+ if (user != null) {
+ _userJson = user;
+ _userId = user['id'] as String?;
+ }
+ final devicesRaw = auth['ereaderDevices'] as List<dynamic>?;
+ if (devicesRaw != null) {
+ _ereaderDevices = devicesRaw.cast<Map<String, dynamic>>();
+ await _persistEreaderDevices();
+ }
+ final sSettings = auth['serverSettings'] as Map<String, dynamic>?;
+ if (sSettings != null) _serverSettings = sSettings;
+ final defaultLib = auth['userDefaultLibraryId'] as String?;
+ if (defaultLib != null) _defaultLibraryId = defaultLib;
+ } else {
+ final me = await api.getMe();
+ if (me != null) {
+ _userJson = me;
+ _userId = me['id'] as String?;
+ }
+ }
+ if (_userJson != null) {
+ debugPrint('[Auth] Loaded user info on reconnect (type=${_userJson?['type']})');
+ notifyListeners();
+ }
+ } catch (e) {
+ debugPrint('[Auth] _ensureUserInfo failed: $e');
+ } finally {
+ _ensuringUserInfo = false;
+ }
+ }
+
/// Login with username/password.
///
/// [l] is used to localize error messages stored in [errorMessage]. If null,