FIX: VIEW REGISTER
This commit is contained in:
@@ -1,25 +1,22 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter_application_1/features/auth/presentation/screens/register_screen.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../features/auth/presentation/bloc/auth_bloc.dart';
|
||||
import '../../features/auth/presentation/bloc/auth_state.dart';
|
||||
import '../../features/auth/presentation/screens/login_screen.dart';
|
||||
import '../../features/auth/presentation/screens/register_screen.dart';
|
||||
import '../../features/auth/presentation/screens/home_screen_placeholder.dart';
|
||||
|
||||
/// Rutas nombradas de la aplicación.
|
||||
abstract final class AppRoutes {
|
||||
static const String splash = '/';
|
||||
static const String login = '/login';
|
||||
static const String register = '/register'; // 📍 Agregada constante oficial
|
||||
static const String home = '/home';
|
||||
}
|
||||
|
||||
/// Configuración central de navegación con go_router.
|
||||
///
|
||||
/// La redirección basada en estado de autenticación garantiza que
|
||||
/// rutas protegidas sean inaccesibles sin sesión válida.
|
||||
GoRouter createRouter(AuthBloc authBloc) {
|
||||
return GoRouter(
|
||||
initialLocation: AppRoutes.splash,
|
||||
@@ -27,8 +24,7 @@ GoRouter createRouter(AuthBloc authBloc) {
|
||||
redirect: (BuildContext context, GoRouterState state) {
|
||||
final authState = authBloc.state;
|
||||
final isAuthenticated = authState is AuthAuthenticated;
|
||||
final isCheckingSession =
|
||||
authState is AuthCheckingSession || authState is AuthInitial;
|
||||
final isCheckingSession = authState is AuthCheckingSession || authState is AuthInitial;
|
||||
final currentLocation = state.uri.path;
|
||||
|
||||
// Mientras se verifica la sesión, mostrar splash.
|
||||
@@ -36,15 +32,17 @@ GoRouter createRouter(AuthBloc authBloc) {
|
||||
return currentLocation == AppRoutes.splash ? null : AppRoutes.splash;
|
||||
}
|
||||
|
||||
// Si no está autenticado, ir a login.
|
||||
// 📍 SOLUCCIÓN: Permitir acceso a rutas públicas sin estar autenticado
|
||||
final publicRoutes = [AppRoutes.login, AppRoutes.register];
|
||||
final isPublicRoute = publicRoutes.contains(currentLocation);
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return currentLocation == AppRoutes.login ? null : AppRoutes.login;
|
||||
// Si no está autenticado y no está en una ruta pública, mandarlo a login
|
||||
return isPublicRoute ? null : AppRoutes.login;
|
||||
}
|
||||
|
||||
// Si está autenticado y en splash o login, ir a home.
|
||||
if (isAuthenticated &&
|
||||
(currentLocation == AppRoutes.login ||
|
||||
currentLocation == AppRoutes.splash)) {
|
||||
// Si está autenticado e intenta ir a login o registro, mandarlo a home
|
||||
if (isAuthenticated && isPublicRoute) {
|
||||
return AppRoutes.home;
|
||||
}
|
||||
|
||||
@@ -60,21 +58,13 @@ GoRouter createRouter(AuthBloc authBloc) {
|
||||
builder: (context, state) => const LoginScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.home,
|
||||
builder: (context, state) => const HomeScreenPlaceholder(),
|
||||
path: AppRoutes.register, // 📍 Usando la constante limpia
|
||||
builder: (context, state) => const RegisterScreen(),
|
||||
),
|
||||
// 📍 Agrega el import arriba si te lo pide:
|
||||
// import 'package:flutter_application_1/features/auth/presentation/screens/register_screen.dart';
|
||||
|
||||
GoRoute(
|
||||
path: AppRoutes.home,
|
||||
builder: (context, state) => const HomeScreenPlaceholder(),
|
||||
),
|
||||
// 📍 CORRECCIÓN: Usamos la constante oficial del proyecto en lugar del texto a mano
|
||||
GoRoute(
|
||||
path: '/register', // 📍 CORRECCIÓN: Texto plano limpio entre comillas
|
||||
builder: (context, state) => const RegisterScreen(),
|
||||
), // GoRoute
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -91,11 +81,7 @@ class _SplashScreen extends StatelessWidget {
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.recycling_rounded,
|
||||
size: 72,
|
||||
color: Colors.white,
|
||||
),
|
||||
const Icon(Icons.recycling_rounded, size: 72, color: Colors.white),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
'WasteNotify',
|
||||
@@ -117,7 +103,6 @@ class _SplashScreen extends StatelessWidget {
|
||||
}
|
||||
|
||||
/// Notificador que conecta el estado del BLoC con go_router.
|
||||
/// Permite que el router reaccione automáticamente a cambios de sesión.
|
||||
class GoRouterAuthNotifier extends ChangeNotifier {
|
||||
final AuthBloc _authBloc;
|
||||
late final StreamSubscription<AuthState> _subscription;
|
||||
|
||||
Reference in New Issue
Block a user