From b93a18448a838855d11fa9e03c2e70a8bc2425b6 Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Mon, 10 Jun 2024 10:05:22 +0700 Subject: [PATCH] Handle error for Twake welcome screen --- lib/pages/bootstrap/bootstrap_dialog.dart | 4 +- lib/pages/bootstrap/tom_bootstrap_dialog.dart | 4 +- lib/pages/twake_welcome/twake_welcome.dart | 36 ++++++++------- .../mixins/connect_page_mixin.dart | 45 +++++++++++++------ 4 files changed, 56 insertions(+), 33 deletions(-) diff --git a/lib/pages/bootstrap/bootstrap_dialog.dart b/lib/pages/bootstrap/bootstrap_dialog.dart index bf436c1a06..0e51f627f1 100644 --- a/lib/pages/bootstrap/bootstrap_dialog.dart +++ b/lib/pages/bootstrap/bootstrap_dialog.dart @@ -106,7 +106,7 @@ class BootstrapDialogState extends State { centerTitle: true, leading: IconButton( icon: const Icon(Icons.close), - onPressed: Navigator.of(context).pop, + onPressed: () => Navigator.of(context).pop(false), ), title: Text(L10n.of(context)!.recoveryKey), ), @@ -233,7 +233,7 @@ class BootstrapDialogState extends State { centerTitle: true, leading: IconButton( icon: const Icon(Icons.close), - onPressed: Navigator.of(context).pop, + onPressed: () => Navigator.of(context).pop(false), ), title: Text(L10n.of(context)!.chatBackup), ), diff --git a/lib/pages/bootstrap/tom_bootstrap_dialog.dart b/lib/pages/bootstrap/tom_bootstrap_dialog.dart index 6686371fe2..d66f994a66 100644 --- a/lib/pages/bootstrap/tom_bootstrap_dialog.dart +++ b/lib/pages/bootstrap/tom_bootstrap_dialog.dart @@ -111,7 +111,7 @@ class TomBootstrapDialogState extends State Logs().d( 'TomBootstrapDialog::_initializeRecoveryKeyState(): no recovery existed then call bootstrap', ); - Navigator.pop(context); + Navigator.of(context, rootNavigator: false).pop(false); await BootstrapDialog(client: widget.client).show(); } } @@ -197,7 +197,7 @@ class TomBootstrapDialogState extends State break; case UploadRecoveryKeyState.unlockError: WidgetsBinding.instance.addPostFrameCallback((_) async { - Navigator.pop(context); + Navigator.of(context, rootNavigator: false).pop(false); await BootstrapDialog(client: widget.client).show(); }); break; diff --git a/lib/pages/twake_welcome/twake_welcome.dart b/lib/pages/twake_welcome/twake_welcome.dart index baf1b19730..23b8867089 100644 --- a/lib/pages/twake_welcome/twake_welcome.dart +++ b/lib/pages/twake_welcome/twake_welcome.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:fluffychat/config/app_config.dart'; import 'package:equatable/equatable.dart'; import 'package:fluffychat/presentation/mixins/connect_page_mixin.dart'; @@ -68,21 +70,25 @@ class TwakeWelcomeController extends State with ConnectPageMixin { } void _redirectRegistrationUrl(String url) async { - final homeserverExisted = await _homeserverExisted(); - if (homeserverExisted) return; - matrix.loginHomeserverSummary = - await matrix.getLoginClient().checkHomeserver( - Uri.parse(AppConfig.twakeWorkplaceHomeserver), - ); - final uri = await FlutterWebAuth2.authenticate( - url: url, - callbackUrlScheme: AppConfig.appOpenUrlScheme, - options: const FlutterWebAuth2Options( - intentFlags: ephemeralIntentFlags, - ), - ); - Logs().d("TwakeIdController:_redirectRegistrationUrl: URI - $uri"); - handleTokenFromRegistrationSite(matrix: matrix, uri: uri); + try { + final homeserverExisted = await _homeserverExisted(); + if (homeserverExisted) return; + matrix.loginHomeserverSummary = + await matrix.getLoginClient().checkHomeserver( + Uri.parse(AppConfig.twakeWorkplaceHomeserver), + ); + final uri = await FlutterWebAuth2.authenticate( + url: url, + callbackUrlScheme: AppConfig.appOpenUrlScheme, + options: const FlutterWebAuth2Options( + intentFlags: ephemeralIntentFlags, + ), + ); + Logs().d("TwakeIdController:_redirectRegistrationUrl: URI - $uri"); + await handleTokenFromRegistrationSite(matrix: matrix, uri: uri); + } catch (e) { + Logs().e("TwakeIdController::_redirectRegistrationUrl: $e"); + } } void onClickCreateTwakeId() { diff --git a/lib/presentation/mixins/connect_page_mixin.dart b/lib/presentation/mixins/connect_page_mixin.dart index 81b6883ce1..de2aeaf7a9 100644 --- a/lib/presentation/mixins/connect_page_mixin.dart +++ b/lib/presentation/mixins/connect_page_mixin.dart @@ -254,23 +254,40 @@ mixin ConnectPageMixin { return list; } - void handleTokenFromRegistrationSite({ + Future handleTokenFromRegistrationSite({ required MatrixState matrix, required String uri, }) async { - final token = Uri.parse(uri).queryParameters['loginToken']; - Logs().d( - "ConnectPageMixin: handleTokenFromRegistrationSite: token: $token", - ); - if (token == null || token.isEmpty == true) return; - matrix.loginType = LoginType.mLoginToken; - await TwakeDialog.showFutureLoadingDialogFullScreen( - future: () => matrix.getLoginClient().login( - LoginType.mLoginToken, - token: token, - initialDeviceDisplayName: PlatformInfos.clientName, - ), - ); + try { + final token = Uri.parse(uri).queryParameters['loginToken']; + Logs().d( + "ConnectPageMixin: handleTokenFromRegistrationSite: token: $token", + ); + if (token == null || token.isEmpty == true) { + return SsoLoginState.tokenEmpty; + } + matrix.loginType = LoginType.mLoginToken; + await TwakeDialog.showStreamDialogFullScreen( + future: () => matrix + .getLoginClient() + .login( + LoginType.mLoginToken, + token: token, + initialDeviceDisplayName: PlatformInfos.clientName, + ) + .timeout( + AutoHomeserverPickerController.autoHomeserverPickerTimeout, + onTimeout: () { + throw CheckHomeserverTimeoutException(); + }, + ), + ); + return SsoLoginState.success; + } catch (e) { + Logs() + .e('ConnectPageMixin:: handleTokenFromRegistrationSite(): error: $e'); + return SsoLoginState.error; + } } void resetLocationPathWithLoginToken({