Co-authored-by: MENDOZA BALLARDO GAEL RICARDO <gael-meb123@users.noreply.github.com>
Co-authored-by: Azareth-Tr <Azareth-Tr@users.noreply.github.com> Co-authored-by: eddgranados12 <eddgranados12@users.noreply.github.com> vistas de mockup
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../core/network/api_client.dart';
|
||||
import '../core/models/auth_state.dart';
|
||||
import '../core/services/auth_controller.dart';
|
||||
import '../core/storage/secure_storage.dart';
|
||||
import 'bootstrap.dart' as bootstrap;
|
||||
@@ -12,27 +13,28 @@ import '../features/auth/register_page.dart';
|
||||
import '../features/addresses/new_address_page.dart';
|
||||
|
||||
final routerProvider = Provider<GoRouter>((ref) {
|
||||
final authSnapshot = ref.watch(authControllerProvider);
|
||||
final isAuthenticated = authSnapshot.asData?.value.isAuthenticated ?? false;
|
||||
// ValueNotifier used as refreshListenable so GoRouter re-evaluates redirect
|
||||
// without recreating the router (which would unmount widgets mid-request).
|
||||
final notifier = ValueNotifier<int>(0);
|
||||
ref.listen<AsyncValue<AuthState>>(authControllerProvider, (prev, next) {
|
||||
notifier.value++;
|
||||
});
|
||||
ref.onDispose(notifier.dispose);
|
||||
|
||||
return GoRouter(
|
||||
initialLocation: '/home',
|
||||
initialLocation: '/login',
|
||||
refreshListenable: notifier,
|
||||
redirect: (context, state) {
|
||||
final authSnapshot = ref.read(authControllerProvider);
|
||||
final isAuthenticated =
|
||||
authSnapshot.asData?.value.isAuthenticated ?? false;
|
||||
final location = state.matchedLocation;
|
||||
final isAuthRoute = location == '/login' || location == '/register';
|
||||
|
||||
if (authSnapshot.isLoading) {
|
||||
return location == '/login' ? null : '/login';
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return isAuthRoute ? null : '/login';
|
||||
}
|
||||
|
||||
if (isAuthenticated && isAuthRoute) {
|
||||
return '/home';
|
||||
}
|
||||
final isAuthRoute =
|
||||
location == '/login' || location == '/register';
|
||||
|
||||
if (authSnapshot.isLoading) return null;
|
||||
if (!isAuthenticated && !isAuthRoute) return '/login';
|
||||
if (isAuthenticated && isAuthRoute) return '/home';
|
||||
return null;
|
||||
},
|
||||
routes: <RouteBase>[
|
||||
|
||||
Reference in New Issue
Block a user