From bc857a00fdbf787faa9026f451729f78b1061441 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 28 Nov 2026 21:51:13 +0530 Subject: [PATCH 1/2] intervals: let a closed browser end the sign-in instead of freezing connected apps --- lib/integrations/intervals_icu.dart | 8 +++++++- lib/ui2/profile/connected_apps.dart | 9 +++++++-- test/integrations/intervals_icu_test.dart | 12 ++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/integrations/intervals_icu.dart b/lib/integrations/intervals_icu.dart index 3f96d5bcb..1dad57078 100644 --- a/lib/integrations/intervals_icu.dart +++ b/lib/integrations/intervals_icu.dart @@ -180,10 +180,16 @@ 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(); + /// 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 got = await (_signIn = LoopbackAuth(host: 'localhost')).run( open: open, authorize: (redirect, state, challenge) => Uri.parse('$_base/oauth/authorize').replace(queryParameters: { 'client_id': kIntervalsClientId, diff --git a/lib/ui2/profile/connected_apps.dart b/lib/ui2/profile/connected_apps.dart index 7f6d4822a..5c8665f36 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())); + }); } From aee3757cc226b421c7f67fcb4021777b7941d041 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 28 Nov 2026 23:01:23 +0530 Subject: [PATCH 2/2] intervals: a cancelled sign-in no longer stores credentials after the token exchange --- lib/integrations/intervals_icu.dart | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/integrations/intervals_icu.dart b/lib/integrations/intervals_icu.dart index 1dad57078..1bdad4996 100644 --- a/lib/integrations/intervals_icu.dart +++ b/lib/integrations/intervals_icu.dart @@ -183,13 +183,17 @@ class IntervalsIcu extends ChangeNotifier { // 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(); + 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.'); _signIn?.cancel(); - final got = await (_signIn = LoopbackAuth(host: 'localhost')).run( + 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, @@ -206,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)); }