diff --git a/lib/pages/auto_homeserver_picker/auto_homeserver_picker.dart b/lib/pages/auto_homeserver_picker/auto_homeserver_picker.dart index 49463bdc2b..5453516490 100644 --- a/lib/pages/auto_homeserver_picker/auto_homeserver_picker.dart +++ b/lib/pages/auto_homeserver_picker/auto_homeserver_picker.dart @@ -3,11 +3,14 @@ import 'package:fluffychat/pages/auto_homeserver_picker/auto_homeserver_picker_s import 'package:fluffychat/pages/auto_homeserver_picker/auto_homeserver_picker_view.dart'; import 'package:fluffychat/presentation/mixins/connect_page_mixin.dart'; import 'package:fluffychat/presentation/mixins/init_config_mixin.dart'; +import 'package:fluffychat/utils/dialog/twake_dialog.dart'; import 'package:fluffychat/utils/exception/homeserver_exception.dart'; +import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:matrix/matrix.dart'; +import 'package:universal_html/html.dart' as html; class AutoHomeserverPicker extends StatefulWidget { final bool? loggedOut; @@ -161,6 +164,25 @@ class AutoHomeserverPickerController extends State Logs().d( "AutoHomeserverPickerController: _initializeAutoHomeserverPicker: PlatForm ${AppConfig.platform}", ); + final loginToken = getQueryParameter('loginToken'); + if (loginToken != null || loginToken?.isNotEmpty == true) { + Matrix.of(context).loginType = LoginType.mLoginToken; + Matrix.of(context).loginHomeserverSummary = + await Matrix.of(context).getLoginClient().checkHomeserver( + Uri.parse( + AppConfig.homeserver, + ), + ); + await TwakeDialog.showFutureLoadingDialogFullScreen( + future: () => Matrix.of(context).getLoginClient().login( + LoginType.mLoginToken, + token: loginToken, + initialDeviceDisplayName: PlatformInfos.clientName, + ), + ); + _resetLocationPath(); + return; + } if (_isSaasPlatform) { _autoConnectSaas(); } else { @@ -173,6 +195,10 @@ class AutoHomeserverPickerController extends State } } + void _resetLocationPath() { + html.window.history.replaceState({}, '', '/#/rooms'); + } + @override void initState() { _setupAutoHomeserverPicker(); diff --git a/lib/pages/login/on_auth_redirect.dart b/lib/pages/login/on_auth_redirect.dart index b0d856b123..eed6c4a390 100644 --- a/lib/pages/login/on_auth_redirect.dart +++ b/lib/pages/login/on_auth_redirect.dart @@ -1,12 +1,12 @@ import 'package:fluffychat/config/app_config.dart'; +import 'package:fluffychat/presentation/mixins/connect_page_mixin.dart'; import 'package:fluffychat/utils/dialog/twake_dialog.dart'; import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/widgets/matrix.dart'; import 'package:fluffychat/widgets/twake_app.dart'; import 'package:flutter/cupertino.dart'; -import 'package:matrix/matrix.dart'; -import 'package:universal_html/html.dart' as html; import 'package:flutter/material.dart'; +import 'package:matrix/matrix.dart'; class OnAuthRedirect extends StatefulWidget { const OnAuthRedirect({super.key}); @@ -15,35 +15,23 @@ class OnAuthRedirect extends StatefulWidget { State createState() => _OnAuthRedirectState(); } -class _OnAuthRedirectState extends State { +class _OnAuthRedirectState extends State with ConnectPageMixin { @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { - tryLoggingUsingToken(); + tryLoggingUsingToken(context: context); }); } - String? getQueryParameter(String key) { - final questionMarkIndex = html.window.location.href.indexOf('?'); - if (questionMarkIndex == -1) { - return null; - } - final queryParams = - Uri.parse(html.window.location.href, questionMarkIndex).queryParameters; - return queryParams[key]; - } - - static bool get homeserverIsConfigured => - AppConfig.homeserver != 'https://example.com/' || - AppConfig.homeserver.isNotEmpty; - - Future tryLoggingUsingToken() async { + Future tryLoggingUsingToken({ + required BuildContext context, + }) async { try { final isConfigured = await AppConfig.initConfigCompleter.future; if (!isConfigured) { if (!AppConfig.hasReachedMaxRetries) { - tryLoggingUsingToken(); + tryLoggingUsingToken(context: context); } else { throw Exception( 'tryLoggingUsingToken(): Config not found', diff --git a/lib/presentation/mixins/connect_page_mixin.dart b/lib/presentation/mixins/connect_page_mixin.dart index 25bbd74b7a..a2d030c169 100644 --- a/lib/presentation/mixins/connect_page_mixin.dart +++ b/lib/presentation/mixins/connect_page_mixin.dart @@ -43,6 +43,20 @@ mixin ConnectPageMixin { bool supportsLogin(BuildContext context) => supportsFlow(context: context, flowType: 'm.login.password'); + String? getQueryParameter(String key) { + final questionMarkIndex = html.window.location.href.indexOf('?'); + if (questionMarkIndex == -1) { + return null; + } + final queryParams = + Uri.parse(html.window.location.href, questionMarkIndex).queryParameters; + return queryParams[key]; + } + + bool get homeserverIsConfigured => + AppConfig.homeserver != 'https://example.com/' || + AppConfig.homeserver.isNotEmpty; + String _getRedirectUrlScheme(String redirectUrl) { return Uri.parse(redirectUrl).scheme; }