From 9fbe84b8c0c2b61e2eceb4457d0d5462949c15bd Mon Sep 17 00:00:00 2001 From: Jonas Martinez <36544012+jonas-martinez@users.noreply.github.com> Date: Fri, 20 Jan 2023 16:37:18 +0100 Subject: [PATCH] fix: Navigation GoRouter reorganized (#71) --- lib/navigator/common_navigator.dart | 102 +++++++++++++--------------- lib/navigator/guard.dart | 48 +++++++------ pubspec.lock | 2 +- pubspec.yaml | 2 +- 4 files changed, 78 insertions(+), 76 deletions(-) diff --git a/lib/navigator/common_navigator.dart b/lib/navigator/common_navigator.dart index 1b5f1b9..fe87cf7 100644 --- a/lib/navigator/common_navigator.dart +++ b/lib/navigator/common_navigator.dart @@ -1,5 +1,4 @@ import 'package:client_common/navigator/guard.dart'; -import 'package:client_common/navigator/page_guard.dart'; import 'package:client_common/views/cgu/cgu_page.dart'; import 'package:client_common/views/cgu/cgu_page_fr.dart'; import 'package:client_common/views/login/login_page.dart'; @@ -23,93 +22,86 @@ class CommonNavigator { static GoRoute changeLostPassword = GoRoute( name: "change-lost", - path: "/change-lost", - builder: (ctx, state) => PageGuard( - guards: [Guard.checkUnauthenticated], - child: ChangeLostPasswordPage(email: state.extra as String), - ), + path: "change-lost", + redirect: (context, state) => Guard.guards(context, [Guard.checkUnauthenticated]), + builder: (ctx, state) => ChangeLostPasswordPage(email: state.extra as String), ); static GoRoute lostPassword = GoRoute( name: "lost", path: "lost", - builder: (ctx, state) => PageGuard( - guards: [Guard.checkUnauthenticated], - child: RecoveryPage(), - ), + redirect: (context, state) => Guard.guards(context, [Guard.checkUnauthenticated]), + builder: (ctx, state) => RecoveryPage(), ); + static GoRoute changePasswordConfirmation = GoRoute( name: "change-confirmation", - path: "/change-confirmation", - builder: (ctx, state) => PageGuard( - guards: [Guard.checkUnauthenticated], - child: ChangePasswordConfirmationPage(), - ), + path: "change-confirmation", + redirect: (context, state) => Guard.guards(context, [Guard.checkUnauthenticated]), + builder: (ctx, state) => ChangePasswordConfirmationPage(), ); + static GoRoute profile = GoRoute( name: "profile", - path: "/profile", - builder: (ctx, state) => PageGuard( - guards: [Guard.checkAuthenticated], - child: ProfilePage(), - ), + path: "profile", + redirect: (context, state) => Guard.guards(context, [Guard.checkAuthenticated]), + builder: (ctx, state) => ProfilePage(), ); + static GoRoute login = GoRoute( name: "login", - path: "/sign", - builder: (ctx, state) => PageGuard( - guards: [Guard.checkUnauthenticated], - child: LoginPage(), - ), + path: "sign", + redirect: (context, state) => Guard.guards(context, [Guard.checkUnauthenticated]), + builder: (ctx, state) => LoginPage(), routes: [lostPassword, register], ); + static GoRoute register = GoRoute( name: "register", path: "register", - builder: (ctx, state) => PageGuard( - guards: [Guard.checkUnauthenticated], - child: RegisterPage(), - ), + redirect: (context, state) => Guard.guards(context, [Guard.checkUnauthenticated]), + builder: (ctx, state) => RegisterPage(), ); + static GoRoute cgu = GoRoute( name: "cgu", - path: "/cgu", - builder: (ctx, state) => PageGuard( - guards: [Guard.checkAuthenticated], - child: CguPage(), - ), + path: "cgu", + redirect: (context, state) => Guard.guards(context, [Guard.checkAuthenticated]), + builder: (ctx, state) => CguPage(), routes: [ cguFR, ], ); + static GoRoute cguFR = GoRoute( name: "cgu-fr", path: "fr", - builder: (ctx, state) => PageGuard( - guards: [Guard.checkAuthenticated], - child: CguPageFr(), - ), + redirect: (context, state) => Guard.guards(context, [Guard.checkAuthenticated]), + builder: (ctx, state) => CguPageFr(), ); + static GoRoute userValidation = GoRoute( name: "validation-user", - path: "/validation-user", - builder: (ctx, state) => PageGuard( - guards: [ - Guard.checkAuthenticated, - Guard.checkCguAccepted, - Guard.checkIsNotUser, - ], - child: VerifyingCodePage(), - ), + path: "validation-user", + redirect: (context, state) => Guard.guards(context, [ + Guard.checkAuthenticated, + Guard.checkCguAccepted, + Guard.checkIsNotUser, + ]), + builder: (ctx, state) => VerifyingCodePage(), + ); + + static ShellRoute authRoutes = ShellRoute( + builder: (context, state, child) => child, + routes: [ + changeLostPassword, + changePasswordConfirmation, + profile, + login, + cgu, + userValidation, + ], ); - static List authRoutes = [ - changeLostPassword, - changePasswordConfirmation, - profile, - login, - cgu, - userValidation, - ]; static void pop(BuildContext context) { GoRouter.of(context).pop(); diff --git a/lib/navigator/guard.dart b/lib/navigator/guard.dart index 0af6d93..c05e2ad 100644 --- a/lib/navigator/guard.dart +++ b/lib/navigator/guard.dart @@ -7,7 +7,6 @@ import 'package:client_common/models/cgu_model.dart'; import 'package:client_common/models/user_application_model.dart'; import 'package:client_common/navigator/common_navigator.dart'; import 'package:flutter/material.dart'; -import 'package:go_router/go_router.dart'; import 'package:provider/provider.dart'; /// This class defines guards that are used to stop the user from accessing certain pages. @@ -29,6 +28,19 @@ class Guard { static final Guard checkIsUser = Guard(isValid: _isUser, onInvalid: _becomeUser); static final Guard checkIsNotUser = Guard(isValid: _isNotUser, onInvalid: _toHome); + static Future guards(BuildContext context, List guards) async { + for (Guard checker in guards) { + try { + if (!await checker.isValid(context)) { + return checker.onInvalid(context); + } + } catch (e) { + return checker.onInvalid(context); + } + } + return null; + } + static Future Function(BuildContext) _isAuthenticated(bool mustBeAuthenticated) { return (BuildContext context) async { AuthModel authModel = context.read(); @@ -38,14 +50,8 @@ class Guard { } catch (e) { return authModel.isAuthenticated() == mustBeAuthenticated; } - /*if (authModel.refreshStatus.isNone()) - // Try to auth user with refresh token - await authModel.refresh().catchError((e) => null); - else if (authModel.refreshStatus.isFetching()) - // Wait current refresh response - await authModel.refreshStatus.wait().catchError((e) => null);*/ } - // then check everything + return authModel.isAuthenticated() == mustBeAuthenticated; }; } @@ -89,25 +95,29 @@ class Guard { }; } - static void _toLogin(BuildContext context) { - context.read().redirectToRoute = CommonNavigator.currentLocation(context); + static String _toLogin(BuildContext context) { + try { + context.read().redirectToRoute = CommonNavigator.currentLocation(context); + } catch (_) { + context.read().redirectToRoute = "/"; + } - CommonNavigator.go(context, CommonNavigator.login); + return "/${CommonNavigator.login.path}"; } - static void _becomeDev(context) { - CommonNavigator.goPath(context, CommonNavigator.validationDevRoute); + static String _becomeDev(context) { + return CommonNavigator.validationDevRoute; } - static void _toHome(context) { - GoRouter.of(context).go(CommonNavigator.homeRoute); + static String _toHome(context) { + return CommonNavigator.homeRoute; } - static void _toCgu(context) { - CommonNavigator.go(context, CommonNavigator.cgu); + static String _toCgu(context) { + return "/${CommonNavigator.cgu.path}"; } - static void _becomeUser(context) { - CommonNavigator.go(context, CommonNavigator.userValidation); + static String _becomeUser(context) { + return "/${CommonNavigator.userValidation.path}"; } } diff --git a/pubspec.lock b/pubspec.lock index 404694d..cc3269f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -148,7 +148,7 @@ packages: name: go_router url: "https://pub.dartlang.org" source: hosted - version: "5.2.4" + version: "6.0.1" html: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 039f00e..7100f76 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,7 +27,7 @@ dependencies: phoenix_wings: ^1.0.2 logging: ^1.0.2 flutter_markdown: ^0.6.10+2 - go_router: ^5.2.4 + go_router: ^6.0.1 flutter: uses-material-design: true