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 Jan 2, 2024
1 parent 0c0bb1b commit 4746fa2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
31 changes: 21 additions & 10 deletions lib/app/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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 @@ -28,20 +28,31 @@ class AppRouter extends _$AppRouter {
transitionsBuilder: TransitionsBuilders.slideLeftWithFade,
);

@override
Future<void> onNavigation(
NavigationResolver resolver,
StackRouter router,
) async {
final authState = ref.read(userProvider).valueOrNull;

if (authState != null || 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
17 changes: 14 additions & 3 deletions lib/features/auth/presentation/auth_page/auth_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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 @@ -38,7 +38,7 @@ class AuthPage extends ConsumerWidget {
.authenticate();

if (context.mounted) {
await context.router.push(const DashboardRoute());
await context.go();
}
},
icon: const Icon(Icons.g_mobiledata),
Expand All @@ -52,7 +52,7 @@ class AuthPage extends ConsumerWidget {
.anonymous();

if (context.mounted) {
await context.router.push(const DashboardRoute());
await context.go();
}
},
icon: const Icon(Icons.person),
Expand All @@ -67,3 +67,14 @@ class AuthPage extends ConsumerWidget {
);
}
}

extension _Go on BuildContext {
Future<void> go() async {
// if we were redirected here, go back to the right page.
await router.pop<bool>(true);
if (mounted) {
// otherwise, go to the dashboard.
await router.push(const DashboardRoute());
}
}
}

0 comments on commit 4746fa2

Please sign in to comment.