mirror of
https://github.com/lucavenir/go_router_riverpod.git
synced 2025-08-26 15:31:23 +08:00
Applied more Riverpod best practices
This commit is contained in:
@ -43,7 +43,7 @@ class HomePage extends ConsumerWidget {
|
|||||||
const Text("Home Page"),
|
const Text("Home Page"),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
ref.read(authNotifierProvider.notifier).logout();
|
ref.watch(authNotifierProvider.notifier).logout();
|
||||||
},
|
},
|
||||||
child: const Text("Logout"),
|
child: const Text("Logout"),
|
||||||
),
|
),
|
||||||
@ -69,7 +69,7 @@ class LoginPage extends ConsumerWidget {
|
|||||||
const Text("Login Page"),
|
const Text("Login Page"),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
ref.read(authNotifierProvider.notifier).login(
|
ref.watch(authNotifierProvider.notifier).login(
|
||||||
"myEmail",
|
"myEmail",
|
||||||
"myPassword",
|
"myPassword",
|
||||||
);
|
);
|
||||||
|
@ -33,30 +33,26 @@ class HomeRoute extends GoRouteData {
|
|||||||
FutureOr<String?> redirect(BuildContext context, GoRouterState state) async {
|
FutureOr<String?> redirect(BuildContext context, GoRouterState state) async {
|
||||||
if (state.location == HomeRoute.path) return null;
|
if (state.location == HomeRoute.path) return null;
|
||||||
|
|
||||||
final asyncRole =
|
final roleListener = ProviderScope.containerOf(context).listen(
|
||||||
ProviderScope.containerOf(context).read(permissionsProvider);
|
permissionsProvider.select((value) => value.valueOrNull),
|
||||||
|
(previous, next) {},
|
||||||
|
);
|
||||||
|
|
||||||
final userRole = asyncRole.valueOrNull;
|
final userRole = roleListener.read();
|
||||||
if (userRole == null) return HomeRoute.path;
|
final redirectTo = userRole?.maybeMap(
|
||||||
|
|
||||||
return userRole.map(
|
|
||||||
admin: (_) => null,
|
admin: (_) => null,
|
||||||
user: (_) {
|
user: (_) {
|
||||||
if (state.location == AdminRoute.path) return HomeRoute.path;
|
if (state.location == AdminRoute.path) return HomeRoute.path;
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
guest: (_) {
|
orElse: () {
|
||||||
if (state.location == AdminRoute.path ||
|
|
||||||
state.location == UserRoute.path) {
|
|
||||||
return HomeRoute.path;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
none: (_) {
|
|
||||||
if (state.location != HomeRoute.path) return HomeRoute.path;
|
if (state.location != HomeRoute.path) return HomeRoute.path;
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
roleListener.close();
|
||||||
|
return redirectTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -44,7 +44,7 @@ class HomePage extends ConsumerWidget {
|
|||||||
const Text("Home Page"),
|
const Text("Home Page"),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
ref.read(authNotifierProvider.notifier).logout();
|
ref.watch(authNotifierProvider.notifier).logout();
|
||||||
},
|
},
|
||||||
child: const Text("Logout"),
|
child: const Text("Logout"),
|
||||||
),
|
),
|
||||||
@ -71,7 +71,7 @@ class LoginPage extends ConsumerWidget {
|
|||||||
const Text("Login Page"),
|
const Text("Login Page"),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
ref.read(authNotifierProvider.notifier).login(
|
ref.watch(authNotifierProvider.notifier).login(
|
||||||
"myEmail",
|
"myEmail",
|
||||||
"myPassword",
|
"myPassword",
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user