feat(01-11): integrate router into main app with error handling
- Updated main.dart to use ProviderScope and MaterialApp.router - Integrated AppRouter with proper GoRouter configuration - Added comprehensive error handling with ErrorBoundary widget - Updated HomePage to use auth provider for logout functionality - Fixed SplashPage to use GoRouter navigation - Implemented router provider for Riverpod integration - Added proper resource disposal and restart functionality Files modified: - lib/main.dart (complete app structure) - lib/core/router/app_router.dart (router integration) - lib/features/authentication/presentation/pages/splash_page.dart (navigation fix) - lib/features/home/presentation/pages/home_page.dart (logout implementation)
This commit is contained in:
@@ -13,19 +13,13 @@ import '../../features/authentication/presentation/pages/splash_page.dart';
|
||||
///
|
||||
/// Handles navigation with authentication state awareness and protected routes
|
||||
class AppRouter {
|
||||
static final GoRouter _router = GoRouter(
|
||||
initialLocation: '/',
|
||||
debugLogDiagnostics: true,
|
||||
redirect: (context, state) {
|
||||
final authState = state.extra as AuthState?;
|
||||
|
||||
// If no auth state in extra, check from provider
|
||||
if (authState == null) {
|
||||
final container = ProviderScope.containerOf(context, listen: false);
|
||||
final authNotifier = container.read(authStateProvider.notifier);
|
||||
|
||||
// For now, we'll use Supabase directly for auth state checking
|
||||
// This will be improved when auth provider is fully integrated
|
||||
static GoRouter _router({required WidgetRef ref}) {
|
||||
return GoRouter(
|
||||
initialLocation: '/',
|
||||
debugLogDiagnostics: true,
|
||||
redirect: (context, state) {
|
||||
// For now, use Supabase directly for auth state checking
|
||||
// This will be improved when auth provider integration is complete
|
||||
final currentUser = Supabase.instance.client.auth.currentUser;
|
||||
|
||||
// Allow splash page regardless of auth state
|
||||
@@ -44,47 +38,28 @@ class AppRouter {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO: Update to use AuthState when fully integrated
|
||||
// For now, use Supabase auth state directly
|
||||
final currentUser = Supabase.instance.client.auth.currentUser;
|
||||
|
||||
if (currentUser == null && !state.location.startsWith('/login') && !state.location.startsWith('/signup')) {
|
||||
return '/login';
|
||||
}
|
||||
|
||||
if (currentUser != null && (state.location.startsWith('/login') || state.location.startsWith('/signup'))) {
|
||||
return '/home';
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
},
|
||||
routes: [
|
||||
// Splash route - initial loading screen
|
||||
GoRoute(
|
||||
path: '/splash',
|
||||
name: 'splash',
|
||||
builder: (context, state) => const SplashPage(),
|
||||
),
|
||||
|
||||
// Authentication routes (public)
|
||||
GoRoute(
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
builder: (context, state) => const LoginPage(),
|
||||
),
|
||||
|
||||
GoRoute(
|
||||
path: '/signup',
|
||||
name: 'signup',
|
||||
builder: (context, state) => const SignupPage(),
|
||||
),
|
||||
|
||||
// Protected routes (require authentication)
|
||||
GoRoute(
|
||||
path: '/home',
|
||||
name: 'home',
|
||||
builder: (context, state) => const HomePage(),
|
||||
),
|
||||
|
||||
@@ -101,7 +76,6 @@ class AppRouter {
|
||||
// Example:
|
||||
// GoRoute(
|
||||
// path: '/inventory',
|
||||
// name: 'inventory',
|
||||
// builder: (context, state) => const InventoryPage(),
|
||||
// ),
|
||||
],
|
||||
@@ -150,10 +124,10 @@ class AppRouter {
|
||||
);
|
||||
|
||||
/// Get the router instance
|
||||
static GoRouter get router => _router;
|
||||
static GoRouter router({required WidgetRef ref}) => _router(ref: ref);
|
||||
}
|
||||
|
||||
/// Router provider for Riverpod integration
|
||||
final routerProvider = Provider<GoRouter>((ref) {
|
||||
return AppRouter.router;
|
||||
return AppRouter.router(ref: ref);
|
||||
});
|
||||
Reference in New Issue
Block a user