diff --git a/lib/presentation/mixins/connect_page_mixin.dart b/lib/presentation/mixins/connect_page_mixin.dart index de2aeaf7a..62f3bf02c 100644 --- a/lib/presentation/mixins/connect_page_mixin.dart +++ b/lib/presentation/mixins/connect_page_mixin.dart @@ -7,6 +7,7 @@ import 'package:fluffychat/pages/connect/sso_login_state.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/utils/string_extension.dart'; import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -216,9 +217,9 @@ mixin ConnectPageMixin { String _generatePostLogoutRedirectUrl() { if (kIsWeb) { if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { - return '${html.window.origin!}/twake-on-matrix/${AppConfig.issueId}/auth.html'; + return '${html.window.location.href.getBaseUrlBeforeHash()}/twake-on-matrix/${AppConfig.issueId}/auth.html'; } - return '${html.window.origin!}/web/auth.html'; + return '${html.window.location.href.getBaseUrlBeforeHash()}web/auth.html'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://redirect'; } @@ -230,9 +231,9 @@ mixin ConnectPageMixin { homeserverParam = '?homeserver=$homeserver'; } if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { - return '${html.window.origin!}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; + return '${html.window.location.href.getBaseUrlBeforeHash()}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; } - return '${html.window.origin!}/web/auth.html$homeserverParam'; + return '${html.window.location.href.getBaseUrlBeforeHash()}web/auth.html$homeserverParam'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://login'; } @@ -295,6 +296,10 @@ mixin ConnectPageMixin { }) { final loginTokenExisted = getQueryParameter('loginToken') != null; if (!loginTokenExisted) return; - html.window.history.replaceState({}, '', '/#/${route ?? 'rooms'}'); + html.window.history.replaceState( + {}, + '', + '${html.window.location.href.getBaseUrlBeforeHash()}#/${route ?? 'rooms'}', + ); } } diff --git a/lib/utils/string_extension.dart b/lib/utils/string_extension.dart index 02d326a30..2bde62a82 100644 --- a/lib/utils/string_extension.dart +++ b/lib/utils/string_extension.dart @@ -376,4 +376,9 @@ extension StringCasingExtension on String { final match = regex.firstMatch(this); return match?.group(1); } + + String getBaseUrlBeforeHash() { + final fragmentIndex = indexOf('#/'); + return fragmentIndex != -1 ? substring(0, fragmentIndex) : this; + } } diff --git a/test/utils/string_extension_test.dart b/test/utils/string_extension_test.dart index 96fcbda0c..1d34c468d 100644 --- a/test/utils/string_extension_test.dart +++ b/test/utils/string_extension_test.dart @@ -599,4 +599,42 @@ void main() { } }); }); + + group('getBaseUrlBeforeHash test', () { + test('getBaseUrlBeforeHash handles URL with hash', () { + const url = 'https://example.com/web/f/#/test'; + const expectedUrl = 'https://example.com/web/f/'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL without hash', () { + const url = 'https://example.com/test'; + const expectedUrl = 'https://example.com/test'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL with multiple hashes', () { + const url = 'https://example.com/#/test#section'; + const expectedUrl = 'https://example.com/'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL with query parameters and hash', () { + const url = 'https://example.com/test?query=1#/section'; + const expectedUrl = 'https://example.com/test?query=1'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + }); }