From 0e27a38c98ce38f2fbe1adc94950875bb60d0766 Mon Sep 17 00:00:00 2001 From: Eli <88557639+lishaduck@users.noreply.github.com> Date: Thu, 30 Nov 2023 08:34:42 -0600 Subject: [PATCH] refactor: login page "pops" --- lib/app/app_router.dart | 34 +++++++++++++------ .../presentation/auth_page/auth_page.dart | 7 ++-- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/app/app_router.dart b/lib/app/app_router.dart index e5de2087..cf303058 100644 --- a/lib/app/app_router.dart +++ b/lib/app/app_router.dart @@ -11,12 +11,13 @@ import "../features/dashboard/presentation/wrapper_page/wrapper_page.dart"; import "../features/gpa_calculator/presentation/gpa_page/gpa_page.dart"; import "../features/pirate_coins/presentation/pirate_coins_page/pirate_coins_page.dart"; import "../features/pirate_coins/presentation/stats_page/stats_page.dart"; +import "../utils/log.dart"; part "app_router.gr.dart"; /// The router for the application. @AutoRouterConfig(replaceInRouteName: "Page,Route") -class AppRouter extends _$AppRouter { +class AppRouter extends _$AppRouter implements AutoRouteGuard { /// Create a new instance of [AppRouter]. AppRouter({required this.ref}); @@ -26,20 +27,33 @@ class AppRouter extends _$AppRouter { @override final defaultRouteType = const RouteType.material(); + @override + Future onNavigation( + NavigationResolver resolver, + StackRouter router, + ) async { + // final authState = null; + + log.info("${resolver.route.name} (${AuthRoute.name})"); + + if (resolver.route.name == AuthRoute.name) { + resolver.next(); // continue navigation + } else { + // else we navigate to the Login page so we get authenticated + + // tip: use resolver.redirect to have the redirected route + // automatically removed from the stack when the resolver is completed + await resolver.redirect(const AuthRoute()).then( + (didLogin) => resolver.next((didLogin ?? false) as bool), + ); + } + } + @override List get routes => [ AutoRoute( page: WrapperRoute.page, path: "/", - guards: [ - AutoRouteGuard.redirect( - (resolver) { - final authState = ref.read(userProvider).valueOrNull; - - return (authState != null) ? null : const AuthRoute(); - }, - ), - ], children: [ AutoRoute( page: PirateCoinsRoute.page, diff --git a/lib/features/auth/presentation/auth_page/auth_page.dart b/lib/features/auth/presentation/auth_page/auth_page.dart index 29fa9e8f..3cbb1657 100644 --- a/lib/features/auth/presentation/auth_page/auth_page.dart +++ b/lib/features/auth/presentation/auth_page/auth_page.dart @@ -5,12 +5,11 @@ import "package:auto_route/auto_route.dart"; import "package:flutter/material.dart"; import "package:hooks_riverpod/hooks_riverpod.dart"; -import "../../../../app/app_router.dart"; import "../../../../l10n/l10n.dart"; import "../../application/auth_service.dart"; /// The page located at `/login/`. -@RoutePage() +@RoutePage() class AuthPage extends ConsumerWidget { /// Create a new instance of [AuthPage]. const AuthPage({super.key}); @@ -37,7 +36,7 @@ class AuthPage extends ConsumerWidget { .authenticate(); if (context.mounted) { - await context.router.push(const DashboardRoute()); + await context.router.pop(true); } }, icon: const Icon(Icons.g_mobiledata), @@ -51,7 +50,7 @@ class AuthPage extends ConsumerWidget { .anonymous(); if (context.mounted) { - await context.router.push(const DashboardRoute()); + await context.router.pop(true); } }, icon: const Icon(Icons.person),