Skip to content

Commit

Permalink
Admin UI: allow other entrypoints to the Application than /splash (#…
Browse files Browse the repository at this point in the history
…810)

* chore: Update AppDelegate.swift to use @main instead of @NSApplicationMain

* chore: bump libs

* fix: allow redirects in login and splash

* fix: allow other entrypoints to the application than splash

* fix: use matchedLocation instead of fullPath

* refactor: only redirect on authenticated routes

* refactor: only redirect on authenticated routes

* chore: simplify routing
  • Loading branch information
jkoenig134 authored Aug 23, 2024
1 parent d876b36 commit e5a5d62
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
11 changes: 5 additions & 6 deletions Applications/AdminUi/apps/admin_ui/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:admin_api_sdk/admin_api_sdk.dart';
import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand Down Expand Up @@ -35,16 +36,17 @@ final _router = GoRouter(
GoRoute(
parentNavigatorKey: _rootNavigatorKey,
path: '/splash',
builder: (context, state) => const SplashScreen(),
builder: (context, state) => SplashScreen(redirect: state.uri.queryParameters['redirect']),
),
GoRoute(
parentNavigatorKey: _rootNavigatorKey,
path: '/login',
builder: (context, state) => const LoginScreen(),
builder: (context, state) => LoginScreen(redirect: state.uri.queryParameters['redirect']),
),
ShellRoute(
navigatorKey: _shellNavigatorKey,
parentNavigatorKey: _rootNavigatorKey,
redirect: (context, state) => GetIt.I.isRegistered<AdminApiClient>() ? null : '/splash?redirect=${Uri.encodeComponent(state.matchedLocation)}',
routes: [
GoRoute(
parentNavigatorKey: _shellNavigatorKey,
Expand Down Expand Up @@ -102,10 +104,7 @@ final _router = GoRouter(
],
),
],
builder: (context, state, child) => HomeScreen(
location: state.fullPath!,
child: child,
),
builder: (context, state, child) => HomeScreen(location: state.fullPath!, child: child),
),
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'package:shared_preferences/shared_preferences.dart';
import '/core/core.dart';

class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
final String? redirect;

const LoginScreen({required this.redirect, super.key});

@override
State<LoginScreen> createState() => _LoginScreenState();
Expand Down Expand Up @@ -106,6 +108,6 @@ class _LoginScreenState extends State<LoginScreen> {

await GetIt.I.unregisterIfRegistered<AdminApiClient>();
GetIt.I.registerSingleton(await AdminApiClient.create(baseUrl: baseUrl, apiKey: apiKey));
if (mounted) context.go('/identities');
if (mounted) context.go(widget.redirect != null ? Uri.decodeComponent(widget.redirect!) : '/identities');
}
}
31 changes: 16 additions & 15 deletions Applications/AdminUi/apps/admin_ui/lib/screens/splash_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import 'package:shared_preferences/shared_preferences.dart';
import '/core/core.dart';

class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
final String? redirect;

const SplashScreen({required this.redirect, super.key});

@override
State<SplashScreen> createState() => _SplashScreenState();
Expand All @@ -20,7 +22,7 @@ class _SplashScreenState extends State<SplashScreen> {
void initState() {
super.initState();

route();
_route();
}

@override
Expand All @@ -39,28 +41,27 @@ class _SplashScreenState extends State<SplashScreen> {
);
}

Future<void> route() async {
Future<void> _route() async {
await GetIt.I.allReady();
final sp = await SharedPreferences.getInstance();

if (!sp.containsKey('api_key')) {
if (mounted) context.go('/login');

return;
}
if (!sp.containsKey('api_key')) return _navigate('/login');

final apiKey = sp.getString('api_key')!;
const baseUrl = kIsWeb ? '' : String.fromEnvironment('base_url');

final isValid = await AdminApiClient.validateApiKey(baseUrl: baseUrl, apiKey: apiKey);
if (!isValid) {
await sp.remove('api_key');
if (mounted) context.go('/login');

return;
}
if (!isValid) return _navigate('/login');

GetIt.I.registerSingleton(await AdminApiClient.create(baseUrl: baseUrl, apiKey: apiKey));
if (mounted) context.go('/identities');
return _navigate('/identities');
}

void _navigate(String defaultRoute) {
if (!mounted) return;

if (widget.redirect != null) return context.go(Uri.decodeComponent(widget.redirect!));

context.go(defaultRoute);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cocoa
import FlutterMacOS

@NSApplicationMain
@main
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
Expand Down
4 changes: 2 additions & 2 deletions Applications/AdminUi/apps/admin_ui/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
url: "https://pub.dev"
source: hosted
version: "14.2.4"
version: "14.2.5"
watch_it:
dependency: "direct main"
description:
Expand Down

0 comments on commit e5a5d62

Please sign in to comment.