Files
Sage/lib/core/router/app_router.dart
Dani B 1410e32005 feat(01-11): implement auth-aware router and splash screen
- Created AppRouter with GoRouter for declarative navigation
- Added protected routes that redirect to login if not authenticated
- Implemented splash screen with loading state and auth checking
- Set up route definitions for login, signup, home, and splash
- Added error handling for navigation failures
- Includes authentication state-based redirects

Files modified:
- lib/core/router/app_router.dart
- lib/features/authentication/presentation/pages/splash_page.dart
2026-01-28 11:05:02 -05:00

159 lines
5.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../../providers/auth_provider.dart';
import '../../features/authentication/presentation/pages/login_page.dart';
import '../../features/authentication/presentation/pages/signup_page.dart';
import '../../features/home/presentation/pages/home_page.dart';
import '../../features/authentication/presentation/pages/splash_page.dart';
/// Application router configuration
///
/// 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
final currentUser = Supabase.instance.client.auth.currentUser;
// Allow splash page regardless of auth state
if (state.location == '/splash') {
return null;
}
// If not authenticated and trying to access protected route, redirect to login
if (currentUser == null && !state.location.startsWith('/login') && !state.location.startsWith('/signup')) {
return '/login';
}
// If authenticated and on auth pages, redirect to home
if (currentUser != null && (state.location.startsWith('/login') || state.location.startsWith('/signup'))) {
return '/home';
}
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(),
),
// Root route - redirects based on auth state
GoRoute(
path: '/',
redirect: (context, state) {
final currentUser = Supabase.instance.client.auth.currentUser;
return currentUser != null ? '/home' : '/splash';
},
),
// Additional routes will be added here
// Example:
// GoRoute(
// path: '/inventory',
// name: 'inventory',
// builder: (context, state) => const InventoryPage(),
// ),
],
errorBuilder: (context, state) => Scaffold(
appBar: AppBar(
title: const Text('Error'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.error_outline,
size: 64,
color: Colors.red,
),
const SizedBox(height: 16),
Text(
'Page not found',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(
'Could not find: ${state.location}',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.grey[600],
),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () {
// Navigate to home or login based on auth state
final currentUser = Supabase.instance.client.auth.currentUser;
if (currentUser != null) {
context.go('/home');
} else {
context.go('/login');
}
},
child: const Text('Go Home'),
),
],
),
),
),
);
/// Get the router instance
static GoRouter get router => _router;
}
/// Router provider for Riverpod integration
final routerProvider = Provider<GoRouter>((ref) {
return AppRouter.router;
});