34 lines
1.2 KiB
Dart
34 lines
1.2 KiB
Dart
import 'package:go_router/go_router.dart';
|
|
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
|
|
|
|
import '../../features/auth/presentation/bloc/auth_cubit.dart';
|
|
import '../../features/auth/presentation/bloc/auth_state.dart';
|
|
import '../../features/auth/presentation/pages/login_page.dart';
|
|
import '../../features/home/presentation/pages/home_page.dart';
|
|
import '../../features/register/presentation/pages/register_page.dart';
|
|
import 'go_router_refresh_stream.dart';
|
|
|
|
GoRouter createRouter(AuthCubit authCubit) {
|
|
return GoRouter(
|
|
initialLocation: RoutePaths.login,
|
|
refreshListenable: GoRouterRefreshStream(authCubit.stream),
|
|
redirect: (context, state) {
|
|
final loggedIn = authCubit.state is AuthAuthenticated;
|
|
final loggingIn = state.matchedLocation == RoutePaths.login;
|
|
|
|
if (!loggedIn && !loggingIn) {
|
|
return RoutePaths.login;
|
|
}
|
|
if (loggedIn && loggingIn) {
|
|
return RoutePaths.home;
|
|
}
|
|
return null;
|
|
},
|
|
routes: [
|
|
GoRoute(path: RoutePaths.login, builder: (_, __) => LoginPage()),
|
|
GoRoute(path: RoutePaths.register, builder: (_, __) => RegisterPage()),
|
|
GoRoute(path: RoutePaths.home, builder: (context, state) => HomePage()),
|
|
],
|
|
);
|
|
}
|