2026-02-23 19:23:30 +05:30
|
|
|
import 'package:flutter/material.dart';
|
2026-02-24 03:19:55 +05:30
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2026-02-23 19:23:30 +05:30
|
|
|
import 'package:go_router/go_router.dart';
|
2026-02-24 03:19:55 +05:30
|
|
|
import 'package:real_estate_mobile/features/auth/presentation/providers/auth_provider.dart';
|
2026-02-23 19:23:30 +05:30
|
|
|
import 'package:real_estate_mobile/features/auth/presentation/screens/login_screen.dart';
|
|
|
|
|
import 'package:real_estate_mobile/features/auth/presentation/screens/signup_screen.dart';
|
2026-03-07 10:25:53 +05:30
|
|
|
import 'package:real_estate_mobile/features/agents/presentation/screens/agent_search_screen.dart';
|
2026-03-07 23:28:21 +05:30
|
|
|
import 'package:real_estate_mobile/features/faq/presentation/screens/faq_screen.dart';
|
2026-02-24 03:19:55 +05:30
|
|
|
import 'package:real_estate_mobile/features/home/presentation/screens/home_screen.dart';
|
2026-02-23 19:23:30 +05:30
|
|
|
|
2026-02-24 03:19:55 +05:30
|
|
|
final routerProvider = Provider<GoRouter>((ref) {
|
|
|
|
|
final authRefreshNotifier = _AuthRefreshNotifier();
|
2026-02-23 19:23:30 +05:30
|
|
|
|
2026-02-24 03:19:55 +05:30
|
|
|
ref.listen(authProvider, (_, __) {
|
|
|
|
|
authRefreshNotifier.notify();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ref.onDispose(() {
|
|
|
|
|
authRefreshNotifier.dispose();
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-07 23:28:21 +05:30
|
|
|
// Public routes accessible without authentication (matching web middleware)
|
|
|
|
|
const publicRoutes = ['/home', '/agents/search', '/faq'];
|
|
|
|
|
const authRoutes = ['/login', '/signup'];
|
|
|
|
|
|
2026-02-24 03:19:55 +05:30
|
|
|
return GoRouter(
|
2026-03-07 23:28:21 +05:30
|
|
|
initialLocation: '/home',
|
2026-02-24 03:19:55 +05:30
|
|
|
refreshListenable: authRefreshNotifier,
|
|
|
|
|
redirect: (context, state) {
|
|
|
|
|
final authState = ref.read(authProvider);
|
|
|
|
|
final isAuthenticated = authState.status == AuthStatus.authenticated;
|
|
|
|
|
final isLoading = authState.status == AuthStatus.loading;
|
|
|
|
|
|
2026-03-07 23:28:21 +05:30
|
|
|
final location = state.matchedLocation;
|
|
|
|
|
final isAuthRoute = authRoutes.contains(location);
|
|
|
|
|
final isPublicRoute = publicRoutes.any(
|
|
|
|
|
(route) => location == route || location.startsWith('$route/'),
|
|
|
|
|
);
|
2026-02-24 03:19:55 +05:30
|
|
|
|
|
|
|
|
// While checking auth status, don't redirect
|
|
|
|
|
if (isLoading) return null;
|
|
|
|
|
|
2026-03-07 23:28:21 +05:30
|
|
|
// If authenticated and on auth page (login/signup), go to home
|
2026-02-24 03:19:55 +05:30
|
|
|
if (isAuthenticated && isAuthRoute) return '/home';
|
|
|
|
|
|
2026-03-07 23:28:21 +05:30
|
|
|
// Public routes are always accessible
|
|
|
|
|
if (isPublicRoute || isAuthRoute) return null;
|
|
|
|
|
|
|
|
|
|
// Protected routes require authentication — redirect to login
|
|
|
|
|
if (!isAuthenticated) return '/login';
|
2026-02-24 03:19:55 +05:30
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
},
|
2026-02-23 19:23:30 +05:30
|
|
|
routes: [
|
2026-02-24 03:19:55 +05:30
|
|
|
GoRoute(
|
|
|
|
|
path: '/login',
|
|
|
|
|
builder: (context, state) => const LoginScreen(),
|
|
|
|
|
),
|
2026-02-23 19:23:30 +05:30
|
|
|
GoRoute(
|
|
|
|
|
path: '/signup',
|
|
|
|
|
builder: (context, state) => const SignupScreen(),
|
|
|
|
|
),
|
|
|
|
|
GoRoute(
|
2026-02-24 03:19:55 +05:30
|
|
|
path: '/home',
|
|
|
|
|
builder: (context, state) => const HomeScreen(),
|
2026-02-23 19:23:30 +05:30
|
|
|
),
|
2026-03-07 10:25:53 +05:30
|
|
|
GoRoute(
|
|
|
|
|
path: '/agents/search',
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
final query = state.uri.queryParameters['q'];
|
|
|
|
|
return AgentSearchScreen(initialQuery: query);
|
|
|
|
|
},
|
|
|
|
|
),
|
2026-03-07 23:28:21 +05:30
|
|
|
GoRoute(
|
|
|
|
|
path: '/faq',
|
|
|
|
|
builder: (context, state) => const FaqScreen(),
|
|
|
|
|
),
|
2026-02-23 19:23:30 +05:30
|
|
|
],
|
|
|
|
|
);
|
2026-02-24 03:19:55 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/// Notifier that triggers GoRouter refresh when auth state changes.
|
|
|
|
|
class _AuthRefreshNotifier extends ChangeNotifier {
|
|
|
|
|
void notify() {
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
2026-02-23 19:23:30 +05:30
|
|
|
}
|