- Fixed GoRouter state.location -> state.uri API usage - Removed Riverpod dependencies to simplify integration - Rewrote HomePage to use StatelessWidget with Supabase auth - Fixed syntax errors and ambiguous imports in router - Resolved Center widget import conflict Files modified: - lib/core/router/app_router.dart (API fixes) - lib/features/home/presentation/pages/home_page.dart (syntax fixes)
117 lines
3.8 KiB
Dart
117 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.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) {
|
|
// Use Supabase directly for auth state checking
|
|
final currentUser = Supabase.instance.client.auth.currentUser;
|
|
|
|
// Allow splash page regardless of auth state
|
|
if (state.uri.toString() == '/splash') {
|
|
return null;
|
|
}
|
|
|
|
// If not authenticated and trying to access protected route, redirect to login
|
|
if (currentUser == null && !state.uri.toString().startsWith('/login') && !state.uri.toString().startsWith('/signup')) {
|
|
return '/login';
|
|
}
|
|
|
|
// If authenticated and on auth pages, redirect to home
|
|
if (currentUser != null && (state.uri.toString().startsWith('/login') || state.uri.toString().startsWith('/signup'))) {
|
|
return '/home';
|
|
}
|
|
|
|
return null;
|
|
},
|
|
routes: [
|
|
// Splash route - initial loading screen
|
|
GoRoute(
|
|
path: '/splash',
|
|
builder: (context, state) => const SplashPage(),
|
|
),
|
|
|
|
// Authentication routes (public)
|
|
GoRoute(
|
|
path: '/login',
|
|
builder: (context, state) => const LoginPage(),
|
|
),
|
|
|
|
GoRoute(
|
|
path: '/signup',
|
|
builder: (context, state) => const SignupPage(),
|
|
),
|
|
|
|
// Protected routes (require authentication)
|
|
GoRoute(
|
|
path: '/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';
|
|
},
|
|
),
|
|
],
|
|
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.uri.toString()}',
|
|
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) {
|
|
GoRouter.of(context).go('/home');
|
|
} else {
|
|
GoRouter.of(context).go('/login');
|
|
}
|
|
},
|
|
child: const Text('Go Home'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
/// Get the router instance
|
|
static GoRouter get router => _router;
|
|
} |