-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.router.dart
75 lines (70 loc) · 2.42 KB
/
app.router.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import 'package:flutter/foundation.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'constants/current_page.enum.dart';
import 'pages/counter.page.dart';
import 'pages/error.page.dart';
import 'pages/firefly.page.dart';
import 'pages/login.page.dart';
import 'pages/terminal.page.dart';
import 'pages/user_list.page.dart';
import 'pods/auth.pod.dart';
import 'states/auth.state.dart';
final appRouter = Provider<GoRouter>(
(ref) => GoRouter(
debugLogDiagnostics: kDebugMode,
restorationScopeId: 'router',
routes: [
GoRoute(
path: CURRENT_PAGE.counter.toPath(),
builder: (context, state) => const CounterPage(),
),
GoRoute(
path: CURRENT_PAGE.firefly.toPath(),
builder: (context, state) => const FireflyPage(),
),
GoRoute(
path: CURRENT_PAGE.terminal.toPath(),
builder: (context, state) => const TerminalPage(),
),
GoRoute(
path: CURRENT_PAGE.userlist.toPath(),
builder: (context, state) => const UserListPage(),
),
GoRoute(
path: CURRENT_PAGE.login.toPath(),
builder: (context, state) => const LoginPage(),
),
],
errorBuilder: (context, state) => ErrorPage(state.error),
redirect: (context, state) {
// * hack: https://github.com/rrousselGit/river_pod/issues/815
// * use ref.read() instead of ref.watch()
final authStateProvider = ref.read(authStateNotifierPod);
final routeToPath = state.uri.toString();
if (kDebugMode) {
print('|.. _routeToPath: $routeToPath');
print('\\.... _authStatus: ${authStateProvider.status}');
}
// * not just authStatus but also check the path to go as well
if (authStateProvider.status == AuthStatus.unauthenticated &&
routeToPath != CURRENT_PAGE.login.toPath()) {
return CURRENT_PAGE.login.toPath();
}
if (authStateProvider.status == AuthStatus.authenticated &&
routeToPath == CURRENT_PAGE.login.toPath()) {
return CURRENT_PAGE.counter.toPath();
}
// * go_router got to hit this null at the end to finish routing
return null;
},
),
);
// * provide this Listenable to be listened for refreshing of routing
final refreshListenableProvider = Provider(
(ref) => Listenable.merge(
[
ValueNotifier(ref.watch(authStateNotifierPod.select((state) => state))),
],
),
);