From aded2e89cab8b4795a86661ab306bd91e7ff1311 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Fri, 9 Jun 2023 11:39:07 +0200 Subject: [PATCH] feat(neon): Use window location for login on web Signed-off-by: jld3103 --- packages/neon/neon/lib/src/platform/android.dart | 5 +++++ packages/neon/neon/lib/src/platform/linux.dart | 3 +++ packages/neon/neon/lib/src/platform/platform.dart | 2 ++ packages/neon/neon/lib/src/platform/web.dart | 4 ++++ packages/neon/neon/lib/src/router.dart | 8 ++++++++ packages/neon/neon/pubspec.yaml | 1 + 6 files changed, 23 insertions(+) diff --git a/packages/neon/neon/lib/src/platform/android.dart b/packages/neon/neon/lib/src/platform/android.dart index 9dceed83..25fcc980 100644 --- a/packages/neon/neon/lib/src/platform/android.dart +++ b/packages/neon/neon/lib/src/platform/android.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:meta/meta.dart'; import 'package:neon/src/platform/platform.dart'; import 'package:neon/src/utils/exceptions.dart'; @@ -43,6 +45,9 @@ class AndroidNeonPlatform implements NeonPlatform { return p.join((await getExternalStorageDirectory())!.path); } + @override + String? getWindowLocation() => null; + @override void init() {} } diff --git a/packages/neon/neon/lib/src/platform/linux.dart b/packages/neon/neon/lib/src/platform/linux.dart index b244f390..c54e6450 100644 --- a/packages/neon/neon/lib/src/platform/linux.dart +++ b/packages/neon/neon/lib/src/platform/linux.dart @@ -36,6 +36,9 @@ class LinuxNeonPlatform implements NeonPlatform { @override String get userAccessibleAppDataPath => p.join(Platform.environment['HOME']!, 'Neon'); + @override + String? getWindowLocation() => null; + @override void init() { sqfliteFfiInit(); diff --git a/packages/neon/neon/lib/src/platform/platform.dart b/packages/neon/neon/lib/src/platform/platform.dart index d8a688fb..1b61b31e 100644 --- a/packages/neon/neon/lib/src/platform/platform.dart +++ b/packages/neon/neon/lib/src/platform/platform.dart @@ -69,5 +69,7 @@ abstract interface class NeonPlatform { FutureOr get userAccessibleAppDataPath; + String? getWindowLocation(); + FutureOr init(); } diff --git a/packages/neon/neon/lib/src/platform/web.dart b/packages/neon/neon/lib/src/platform/web.dart index 9dc78b6a..0fab9779 100644 --- a/packages/neon/neon/lib/src/platform/web.dart +++ b/packages/neon/neon/lib/src/platform/web.dart @@ -4,6 +4,7 @@ import 'package:meta/meta.dart'; import 'package:neon/src/platform/platform.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:sqflite_common_ffi_web/sqflite_ffi_web.dart'; +import 'package:universal_html/html.dart' as html; @immutable @internal @@ -37,6 +38,9 @@ class WebNeonPlatform implements NeonPlatform { @override FutureOr? get userAccessibleAppDataPath => null; + @override + String getWindowLocation() => html.window.location.href; + @override FutureOr init() { databaseFactory = databaseFactoryFfiWeb; diff --git a/packages/neon/neon/lib/src/router.dart b/packages/neon/neon/lib/src/router.dart index 83c20b18..274d2cf6 100644 --- a/packages/neon/neon/lib/src/router.dart +++ b/packages/neon/neon/lib/src/router.dart @@ -19,6 +19,7 @@ import 'package:neon/src/pages/login_qr_code.dart'; import 'package:neon/src/pages/nextcloud_app_settings.dart'; import 'package:neon/src/pages/route_not_found.dart'; import 'package:neon/src/pages/settings.dart'; +import 'package:neon/src/platform/platform.dart'; import 'package:neon/src/utils/provider.dart'; import 'package:neon/src/utils/stream_listenable.dart'; @@ -54,6 +55,13 @@ GoRouter buildAppRouter({ // redirect to login screen when no account is logged in if (!accountsBloc.hasAccounts && !state.uri.toString().startsWith(const LoginRoute().location)) { + final windowLocation = NeonProvider.of(context).getWindowLocation(); + if (windowLocation != null) { + var uri = Uri.parse(windowLocation).removeFragment(); + uri = uri.replace(path: uri.path.replaceFirst(RegExp(r'/$', multiLine: true), '')); + return LoginCheckServerStatusRoute(serverUrl: uri).location; + } + return const LoginRoute().location; } diff --git a/packages/neon/neon/pubspec.yaml b/packages/neon/neon/pubspec.yaml index 05916de5..642435fe 100644 --- a/packages/neon/neon/pubspec.yaml +++ b/packages/neon/neon/pubspec.yaml @@ -52,6 +52,7 @@ dependencies: tray_manager: ^0.2.0 unifiedpush: ^5.0.0 unifiedpush_android: ^2.0.0 + universal_html: ^2.0.0 universal_io: ^2.0.0 url_launcher: ^6.1.0 vector_graphics: ^1.0.0