Skip to content

Commit

Permalink
refactor: login page "pops"
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Dec 9, 2023
1 parent 2d42097 commit 0e27a38
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
34 changes: 24 additions & 10 deletions lib/app/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand All @@ -26,20 +27,33 @@ class AppRouter extends _$AppRouter {
@override
final defaultRouteType = const RouteType.material();

@override
Future<void> 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<AutoRoute> 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,
Expand Down
7 changes: 3 additions & 4 deletions lib/features/auth/presentation/auth_page/auth_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>()
class AuthPage extends ConsumerWidget {
/// Create a new instance of [AuthPage].
const AuthPage({super.key});
Expand All @@ -37,7 +36,7 @@ class AuthPage extends ConsumerWidget {
.authenticate();

if (context.mounted) {
await context.router.push(const DashboardRoute());
await context.router.pop<bool>(true);
}
},
icon: const Icon(Icons.g_mobiledata),
Expand All @@ -51,7 +50,7 @@ class AuthPage extends ConsumerWidget {
.anonymous();

if (context.mounted) {
await context.router.push(const DashboardRoute());
await context.router.pop<bool>(true);
}
},
icon: const Icon(Icons.person),
Expand Down

0 comments on commit 0e27a38

Please sign in to comment.