diff --git a/lib/integrations/intervals_icu.dart b/lib/integrations/intervals_icu.dart index 3f96d5bcb..1bdad4996 100644 --- a/lib/integrations/intervals_icu.dart +++ b/lib/integrations/intervals_icu.dart @@ -180,10 +180,20 @@ class IntervalsIcu extends ChangeNotifier { await _store(a); } + // Nothing reports a closed in-app browser, so the caller ends a waiting + // sign-in with [cancelSignIn]; a new sign-in also ends the old one. + LoopbackAuth? _signIn; + void cancelSignIn() { + _signIn?.cancel(); + _signIn = null; // also voids a sign-in already in the token exchange + } + /// OAuth PKCE sign-in; [open] shows the page in an in-app browser. Future signIn(Future Function(Uri) open) async { if (!oauthAvailable) throw IntervalsException('intervals.icu sign-in is not available yet. Use an API key.'); - final got = await LoopbackAuth(host: 'localhost').run( + _signIn?.cancel(); + final attempt = _signIn = LoopbackAuth(host: 'localhost'); + final got = await attempt.run( open: open, authorize: (redirect, state, challenge) => Uri.parse('$_base/oauth/authorize').replace(queryParameters: { 'client_id': kIntervalsClientId, @@ -200,6 +210,7 @@ class IntervalsIcu extends ChangeNotifier { 'code_verifier': got.verifier, 'redirect_uri': got.redirectUri, })); + if (!identical(_signIn, attempt)) throw IntervalsException('Sign-in was cancelled.'); if (r.statusCode >= 300) throw IntervalsException('intervals.icu sign-in failed (${r.statusCode}).'); await _store(parseIntervalsToken(r.body)); } diff --git a/lib/ui2/profile/connected_apps.dart b/lib/ui2/profile/connected_apps.dart index d34d117b5..e20f606b3 100644 --- a/lib/ui2/profile/connected_apps.dart +++ b/lib/ui2/profile/connected_apps.dart @@ -39,6 +39,7 @@ class _ConnectedAppsState extends State { @override void dispose() { _intervals.removeListener(_refresh); + _intervals.cancelSignIn(); super.dispose(); } @@ -73,9 +74,13 @@ class _ConnectedAppsState extends State { } /// One wrapper for every action on this screen: a spinner-free busy guard - /// and the failure said in the exception's own words. + /// and the failure said in the exception's own words. A tap while an + /// intervals.icu sign-in waits means the browser was closed: end it. Future _run(Future Function() f) async { - if (_busy) return; + if (_busy) { + _intervals.cancelSignIn(); + return; + } setState(() => _busy = true); try { await f(); diff --git a/test/integrations/intervals_icu_test.dart b/test/integrations/intervals_icu_test.dart index 5d747f1fe..9e8980a5d 100644 --- a/test/integrations/intervals_icu_test.dart +++ b/test/integrations/intervals_icu_test.dart @@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:openstrap_edge/integrations/intervals_icu.dart'; +import 'package:openstrap_edge/integrations/oauth_loopback.dart'; void main() { test('wellness body omits absent fields and converts units', () { @@ -100,4 +101,15 @@ void main() { await i.pushAfterDerive(); expect(calls, 0); }); + + test('closing the browser: cancelSignIn ends a waiting sign-in, a new one ends the old', () async { + final i = IntervalsIcu(client: MockClient((r) async => http.Response('', 500))); + final first = i.signIn((_) async => true); + await Future.delayed(const Duration(milliseconds: 50)); + final second = i.signIn((_) async => true); + await expectLater(first, throwsA(isA())); + await Future.delayed(const Duration(milliseconds: 50)); + i.cancelSignIn(); + await expectLater(second, throwsA(isA())); + }); }