Skip to content

Commit

Permalink
TW-1711: Handle deep link in auto homeserver picker
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev committed Apr 26, 2024
1 parent 624c5ad commit 6184a4f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
26 changes: 26 additions & 0 deletions lib/pages/auto_homeserver_picker/auto_homeserver_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,6 +164,25 @@ class AutoHomeserverPickerController extends State<AutoHomeserverPicker>
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 {
Expand All @@ -173,6 +195,10 @@ class AutoHomeserverPickerController extends State<AutoHomeserverPicker>
}
}

void _resetLocationPath() {
html.window.history.replaceState({}, '', '/#/rooms');
}

@override
void initState() {
_setupAutoHomeserverPicker();
Expand Down
28 changes: 8 additions & 20 deletions lib/pages/login/on_auth_redirect.dart
Original file line number Diff line number Diff line change
@@ -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});
Expand All @@ -15,35 +15,23 @@ class OnAuthRedirect extends StatefulWidget {
State<OnAuthRedirect> createState() => _OnAuthRedirectState();
}

class _OnAuthRedirectState extends State<OnAuthRedirect> {
class _OnAuthRedirectState extends State<OnAuthRedirect> 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<void> tryLoggingUsingToken() async {
Future<void> 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',
Expand Down
14 changes: 14 additions & 0 deletions lib/presentation/mixins/connect_page_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 6184a4f

Please sign in to comment.