mirror of
https://github.com/R0m1k3/xtremobile.git
synced 2026-10-11 17:29:26 +02:00
70 lines
2.1 KiB
Dart
70 lines
2.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../features/auth/providers/auth_provider.dart';
|
|
import '../../features/auth/screens/login_screen.dart';
|
|
import '../../features/iptv/screens/playlist_selection_screen.dart';
|
|
import '../../features/iptv/screens/dashboard_screen.dart';
|
|
import '../../features/admin/screens/admin_panel.dart';
|
|
import '../models/playlist_config.dart';
|
|
|
|
final routerProvider = Provider<GoRouter>((ref) {
|
|
final authNotifier = ref.watch(authProvider.notifier);
|
|
final authState = ref.watch(authProvider);
|
|
|
|
return GoRouter(
|
|
initialLocation: '/login',
|
|
redirect: (context, state) {
|
|
final isLoggedIn = authState.isAuthenticated;
|
|
final isLoginRoute = state.matchedLocation == '/login';
|
|
|
|
// Redirect to login if not authenticated
|
|
if (!isLoggedIn && !isLoginRoute) {
|
|
return '/login';
|
|
}
|
|
|
|
// Redirect to playlists if already logged in and trying to access login
|
|
if (isLoggedIn && isLoginRoute) {
|
|
return '/playlists';
|
|
}
|
|
|
|
// Admin guard for /admin route
|
|
if (state.matchedLocation == '/admin' && !authState.isAdmin) {
|
|
return '/playlists';
|
|
}
|
|
|
|
return null;
|
|
},
|
|
routes: [
|
|
GoRoute(
|
|
path: '/login',
|
|
builder: (context, state) => const LoginScreen(),
|
|
),
|
|
GoRoute(
|
|
path: '/playlists',
|
|
builder: (context, state) => const PlaylistSelectionScreen(),
|
|
),
|
|
GoRoute(
|
|
path: '/dashboard',
|
|
builder: (context, state) {
|
|
final playlist = state.extra as PlaylistConfig?;
|
|
if (playlist == null) {
|
|
// Redirect back if no playlist provided
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.go('/playlists');
|
|
});
|
|
return const Scaffold(
|
|
body: Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
return DashboardScreen(playlist: playlist);
|
|
},
|
|
),
|
|
GoRoute(
|
|
path: '/admin',
|
|
builder: (context, state) => const AdminPanel(),
|
|
),
|
|
],
|
|
);
|
|
});
|