Skip to content

Commit

Permalink
fix: Navigation GoRouter reorganized (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-martinez authored and taorepoara committed Sep 29, 2023
1 parent 3965e99 commit 9fbe84b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 76 deletions.
102 changes: 47 additions & 55 deletions lib/navigator/common_navigator.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<RouteBase> authRoutes = [
changeLostPassword,
changePasswordConfirmation,
profile,
login,
cgu,
userValidation,
];

static void pop(BuildContext context) {
GoRouter.of(context).pop();
Expand Down
48 changes: 29 additions & 19 deletions lib/navigator/guard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<String?> guards(BuildContext context, List<Guard> 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<bool> Function(BuildContext) _isAuthenticated(bool mustBeAuthenticated) {
return (BuildContext context) async {
AuthModel authModel = context.read<AuthModel>();
Expand All @@ -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;
};
}
Expand Down Expand Up @@ -89,25 +95,29 @@ class Guard {
};
}

static void _toLogin(BuildContext context) {
context.read<AuthModel>().redirectToRoute = CommonNavigator.currentLocation(context);
static String _toLogin(BuildContext context) {
try {
context.read<AuthModel>().redirectToRoute = CommonNavigator.currentLocation(context);
} catch (_) {
context.read<AuthModel>().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}";
}
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9fbe84b

Please sign in to comment.