Skip to content

Commit 501aaa6

Browse files
committed
store: Handle invalid API key on register-queue
Fixes: zulip#737 Signed-off-by: Zixuan James Li <[email protected]>
1 parent 90e4159 commit 501aaa6

14 files changed

+338
-7
lines changed

assets/l10n/app_en.arb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,13 @@
458458
"@topicValidationErrorMandatoryButEmpty": {
459459
"description": "Topic validation error when topic is required but was empty."
460460
},
461+
"errorInvalidApiKeyMessage": "Your account at {url} could not be authenticated. Please try logging in again or use another account.",
462+
"@errorInvalidApiKeyMessage": {
463+
"description": "Error message in the dialog for invalid API key.",
464+
"placeholders": {
465+
"url": {"type": "String", "example": "http://chat.example.com/"}
466+
}
467+
},
461468
"errorInvalidResponse": "The server sent an invalid response",
462469
"@errorInvalidResponse": {
463470
"description": "Error message when an API call returned an invalid response."

lib/generated/l10n/zulip_localizations.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,12 @@ abstract class ZulipLocalizations {
721721
/// **'Topics are required in this organization.'**
722722
String get topicValidationErrorMandatoryButEmpty;
723723

724+
/// Error message in the dialog for invalid API key.
725+
///
726+
/// In en, this message translates to:
727+
/// **'Your account at {url} could not be authenticated. Please try logging in again or use another account.'**
728+
String errorInvalidApiKeyMessage(String url);
729+
724730
/// Error message when an API call returned an invalid response.
725731
///
726732
/// In en, this message translates to:

lib/generated/l10n/zulip_localizations_ar.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsAr extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_en.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsEn extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_fr.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsFr extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_ja.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsJa extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/generated/l10n/zulip_localizations_pl.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsPl extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Wątki są wymagane przez tę organizację.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'Nieprawidłowa odpowiedź serwera';
362367

lib/generated/l10n/zulip_localizations_ru.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class ZulipLocalizationsRu extends ZulipLocalizations {
357357
@override
358358
String get topicValidationErrorMandatoryButEmpty => 'Topics are required in this organization.';
359359

360+
@override
361+
String errorInvalidApiKeyMessage(String url) {
362+
return 'Your account at $url could not be authenticated. Please try logging in again or use another account.';
363+
}
364+
360365
@override
361366
String get errorInvalidResponse => 'The server sent an invalid response';
362367

lib/model/store.dart

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import '../api/backoff.dart';
1919
import '../api/route/realm.dart';
2020
import '../log.dart';
2121
import '../notifications/receive.dart';
22+
import '../widgets/actions.dart';
2223
import 'autocomplete.dart';
2324
import 'database.dart';
2425
import 'emoji.dart';
@@ -149,13 +150,31 @@ abstract class GlobalStore extends ChangeNotifier {
149150
/// and/or [perAccountSync].
150151
Future<PerAccountStore> loadPerAccount(int accountId) async {
151152
assert(_accounts.containsKey(accountId));
152-
final store = await doLoadPerAccount(accountId);
153+
PerAccountStore? store;
154+
try {
155+
store = await doLoadPerAccount(accountId);
156+
} catch (e) {
157+
switch (e) {
158+
case ZulipApiException(code: 'INVALID_API_KEY'):
159+
// The API key is invalid and the store can never be loaded
160+
// unless the user retries manually.
161+
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
162+
final account = getAccount(accountId)!;
163+
reportErrorToUserModally(
164+
zulipLocalizations.errorCouldNotConnectTitle,
165+
details: zulipLocalizations.errorInvalidApiKeyMessage(
166+
account.realmUrl.toString()));
167+
await logOutAccount(this, accountId);
168+
default:
169+
rethrow;
170+
}
171+
}
153172
if (!_accounts.containsKey(accountId)) {
154-
// [removeAccount] was called during [doLoadPerAccount].
155-
store.dispose();
173+
// [removeAccount] was called during or after [doLoadPerAccount].
174+
store?.dispose();
156175
throw AccountNotFoundException();
157176
}
158-
return store;
177+
return store!;
159178
}
160179

161180
/// Load per-account data for the given account, unconditionally.
@@ -912,7 +931,13 @@ class UpdateMachine {
912931
// at 1 kiB (at least on Android), and stack can be longer than that.
913932
assert(debugLog('Stack:\n$s'));
914933
assert(debugLog('Backing off, then will retry…'));
915-
// TODO tell user if initial-fetch errors persist, or look non-transient
934+
// TODO(#890): tell user if initial-fetch errors persist, or look non-transient
935+
switch (e) {
936+
case ZulipApiException(code: 'INVALID_API_KEY'):
937+
// We cannot recover from this error through retrying.
938+
// Leave it to [GlobalStore.loadPerAccount].
939+
rethrow;
940+
}
916941
await (backoffMachine ??= BackoffMachine()).wait();
917942
assert(debugLog('… Backoff wait complete, retrying initial fetch.'));
918943
}
@@ -1044,7 +1069,13 @@ class UpdateMachine {
10441069
}
10451070
} catch (e) {
10461071
if (_disposed) return;
1047-
await _handlePollError(e);
1072+
try {
1073+
await _handlePollError(e);
1074+
} on AccountNotFoundException {
1075+
// Cannot recover by replacing the store because the account
1076+
// was logged out.
1077+
return;
1078+
}
10481079
assert(_disposed);
10491080
return;
10501081
}

lib/widgets/app.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class _ZulipAppState extends State<ZulipApp> with WidgetsBindingObserver {
189189
final themeData = zulipThemeData(context);
190190
return GlobalStoreWidget(
191191
child: Builder(builder: (context) {
192+
final effectiveNavigatorObservers = [
193+
_EmptyStackNavigatorObserver(),
194+
if (widget.navigatorObservers != null)
195+
...widget.navigatorObservers!,
196+
];
192197
final globalStore = GlobalStoreWidget.of(context);
193198
// TODO(#524) choose initial account as last one used
194199
final initialAccountId = globalStore.accounts.firstOrNull?.id;
@@ -199,7 +204,7 @@ class _ZulipAppState extends State<ZulipApp> with WidgetsBindingObserver {
199204
theme: themeData,
200205

201206
navigatorKey: ZulipApp.navigatorKey,
202-
navigatorObservers: widget.navigatorObservers ?? const [],
207+
navigatorObservers: effectiveNavigatorObservers,
203208
builder: (BuildContext context, Widget? child) {
204209
if (!ZulipApp.ready.value) {
205210
SchedulerBinding.instance.addPostFrameCallback(
@@ -230,6 +235,36 @@ class _ZulipAppState extends State<ZulipApp> with WidgetsBindingObserver {
230235
}
231236
}
232237

238+
class _EmptyStackNavigatorObserver extends NavigatorObserver {
239+
void _pushIfEmpty() async {
240+
final navigator = await ZulipApp.navigator;
241+
bool isEmptyStack = true;
242+
// TODO: find a better way to inspect the navigator stack
243+
navigator.popUntil((route) {
244+
isEmptyStack = false;
245+
return true; // never actually pops
246+
});
247+
if (isEmptyStack) {
248+
unawaited(navigator.push(
249+
MaterialWidgetRoute(page: const ChooseAccountPage())));
250+
}
251+
}
252+
253+
@override
254+
void didRemove(Route<void> route, Route<void>? previousRoute) async {
255+
if (previousRoute == null) {
256+
_pushIfEmpty();
257+
}
258+
}
259+
260+
@override
261+
void didPop(Route<void> route, Route<void>? previousRoute) async {
262+
if (previousRoute == null) {
263+
_pushIfEmpty();
264+
}
265+
}
266+
}
267+
233268
class ChooseAccountPage extends StatelessWidget {
234269
const ChooseAccountPage({super.key});
235270

0 commit comments

Comments
 (0)