diff --git a/frontend/pshared/lib/provider/account.dart b/frontend/pshared/lib/provider/account.dart index b6e458c..644907c 100644 --- a/frontend/pshared/lib/provider/account.dart +++ b/frontend/pshared/lib/provider/account.dart @@ -27,7 +27,7 @@ class AccountProvider extends ChangeNotifier { Resource get resource => _resource; late LocaleProvider _localeProvider; PendingLogin? _pendingLogin; - bool _restoreAttempted = false; + Future? _restoreFuture; Account? get account => _resource.data; PendingLogin? get pendingLogin => _pendingLogin; @@ -35,7 +35,7 @@ class AccountProvider extends ChangeNotifier { bool get isLoading => _resource.isLoading; Object? get error => _resource.error; bool get isReady => (!isLoading) && (account != null); - bool get restoreAttempted => _restoreAttempted; + Future? get restoreFuture => _restoreFuture; Account? currentUser() { final acc = account; @@ -69,7 +69,6 @@ class AccountProvider extends ChangeNotifier { required String password, required String locale, }) async { - _restoreAttempted = true; _setResource(_resource.copyWith(isLoading: true, error: null)); try { final outcome = await AccountService.login(LoginData.build( @@ -98,7 +97,6 @@ class AccountProvider extends ChangeNotifier { void completePendingLogin(Account account) { _pendingLogin = null; - _restoreAttempted = true; _setResource(Resource(data: account, isLoading: false, error: null)); _pickupLocale(account.locale); } @@ -106,7 +104,6 @@ class AccountProvider extends ChangeNotifier { Future isAuthorizationStored() async => AuthorizationService.isAuthorizationStored(); Future restore() async { - _restoreAttempted = true; _setResource(_resource.copyWith(isLoading: true, error: null)); try { final acc = await AccountService.restore(); @@ -145,7 +142,6 @@ class AccountProvider extends ChangeNotifier { } Future logout() async { - _restoreAttempted = true; _setResource(_resource.copyWith(isLoading: true, error: null)); try { await AccountService.logout(); @@ -227,16 +223,11 @@ class AccountProvider extends ChangeNotifier { } } - Future restoreIfPossible() async { - if (_restoreAttempted) return; - _restoreAttempted = true; - final hasAuth = await AuthorizationService.isAuthorizationStored(); - if (!hasAuth) { - _setResource(Resource(data: null, isLoading: false, error: null)); - return; - } - try { + Future restoreIfPossible() { + return _restoreFuture ??= () async { + final hasAuth = await AuthorizationService.isAuthorizationStored(); + if (!hasAuth) return; await restore(); - } catch (_) {} + }(); } } diff --git a/frontend/pweb/lib/pages/loaders/account.dart b/frontend/pweb/lib/pages/loaders/account.dart index ccd1bab..a2f1668 100644 --- a/frontend/pweb/lib/pages/loaders/account.dart +++ b/frontend/pweb/lib/pages/loaders/account.dart @@ -36,6 +36,17 @@ class AccountLoader extends StatelessWidget { }); return const Center(child: CircularProgressIndicator()); } + + if (provider.restoreFuture == null) { + WidgetsBinding.instance.addPostFrameCallback((_) { + provider.restoreIfPossible().catchError((error, stack) { + debugPrint('Account restore failed: $error'); + }); + }); + return const Center(child: CircularProgressIndicator()); + } + + if (provider.isLoading) return const Center(child: CircularProgressIndicator()); if (provider.account == null) { WidgetsBinding.instance.addPostFrameCallback((_) => navigateAndReplace(context, Pages.login)); return const Center(child: CircularProgressIndicator());