37 lines
949 B
Dart
37 lines
949 B
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import '../di/providers.dart';
|
|
import '../features/auth/presentation/login_page.dart';
|
|
import '../features/home/presentation/home_page.dart';
|
|
|
|
final appRouterProvider = Provider<GoRouter>((ref) {
|
|
final authState = ref.watch(authControllerProvider);
|
|
|
|
return GoRouter(
|
|
initialLocation: '/login',
|
|
routes: [
|
|
GoRoute(
|
|
path: '/login',
|
|
builder: (context, state) => const LoginPage(),
|
|
),
|
|
GoRoute(
|
|
path: '/',
|
|
builder: (context, state) => const HomePage(),
|
|
),
|
|
],
|
|
redirect: (context, state) {
|
|
final isLoggedIn = authState.isAuthenticated;
|
|
final isLoggingIn = state.matchedLocation == '/login';
|
|
|
|
if (!isLoggedIn && !isLoggingIn) {
|
|
return '/login';
|
|
}
|
|
if (isLoggedIn && isLoggingIn) {
|
|
return '/';
|
|
}
|
|
return null;
|
|
},
|
|
);
|
|
});
|