From 4ac376b7e7df859e850d29e59a448d64df478273 Mon Sep 17 00:00:00 2001 From: DahoudG <41957584+DahoudG@users.noreply.github.com> Date: Fri, 19 Sep 2025 13:03:09 +0000 Subject: [PATCH] L'authentification Keycloak WebView est maintenant 100% fonctionnelle avec navigation automatique vers le dashboard ! --- .../lib/core/auth/bloc/auth_bloc.dart | 20 +++++++--- .../auth/presentation/pages/login_page.dart | 13 +++++-- .../role_dashboards/visitor_dashboard.dart | 12 +++--- unionflow-mobile-apps/lib/main.dart | 39 +++++++++++-------- 4 files changed, 53 insertions(+), 31 deletions(-) diff --git a/unionflow-mobile-apps/lib/core/auth/bloc/auth_bloc.dart b/unionflow-mobile-apps/lib/core/auth/bloc/auth_bloc.dart index 615ef8b..e6df074 100644 --- a/unionflow-mobile-apps/lib/core/auth/bloc/auth_bloc.dart +++ b/unionflow-mobile-apps/lib/core/auth/bloc/auth_bloc.dart @@ -64,11 +64,12 @@ class AuthUserProfileUpdated extends AuthEvent { /// Événement de callback WebView class AuthWebViewCallback extends AuthEvent { final String callbackUrl; + final User? user; - const AuthWebViewCallback(this.callbackUrl); + const AuthWebViewCallback(this.callbackUrl, {this.user}); @override - List get props => [callbackUrl]; + List get props => [callbackUrl, user]; } // === ÉTATS === @@ -254,10 +255,17 @@ class AuthBloc extends Bloc { try { debugPrint('🔄 Traitement callback WebView...'); - // Traiter le callback et récupérer l'utilisateur - final User user = await KeycloakAuthService.handleWebViewCallback(event.callbackUrl); + // Utiliser l'utilisateur fourni ou traiter le callback + final User user; + if (event.user != null) { + debugPrint('👤 Utilisation des données utilisateur fournies: ${event.user!.fullName}'); + user = event.user!; + } else { + debugPrint('🔄 Traitement du callback URL: ${event.callbackUrl}'); + user = await KeycloakAuthService.handleWebViewCallback(event.callbackUrl); + } - debugPrint('👤 Utilisateur récupéré: ${user.fullName} (${user.primaryRole.displayName})'); + debugPrint('👤 Utilisateur authentifié: ${user.fullName} (${user.primaryRole.displayName})'); // Calculer les permissions effectives debugPrint('🔐 Calcul des permissions effectives...'); @@ -278,7 +286,7 @@ class AuthBloc extends Bloc { accessToken: '', // Token géré par KeycloakWebViewAuthService )); - debugPrint('🎉 Authentification complète réussie'); + debugPrint('🎉 Authentification complète réussie - navigation vers dashboard'); } catch (e, stackTrace) { debugPrint('💥 Erreur authentification: $e'); diff --git a/unionflow-mobile-apps/lib/features/auth/presentation/pages/login_page.dart b/unionflow-mobile-apps/lib/features/auth/presentation/pages/login_page.dart index 6e69a02..8782cf1 100644 --- a/unionflow-mobile-apps/lib/features/auth/presentation/pages/login_page.dart +++ b/unionflow-mobile-apps/lib/features/auth/presentation/pages/login_page.dart @@ -73,11 +73,16 @@ class _LoginPageState extends State builder: (context) => KeycloakWebViewAuthPage( onAuthSuccess: (user) { debugPrint('✅ Authentification réussie pour: ${user.fullName}'); - // Notifier le BLoC du succès - context.read().add(AuthWebViewCallback('success')); - // Fermer la WebView et naviguer vers le dashboard + debugPrint('🔄 Notification du BLoC avec les données utilisateur...'); + + // Notifier le BLoC du succès avec les données utilisateur + context.read().add(AuthWebViewCallback( + 'success', + user: user, + )); + + // Fermer la WebView - la navigation sera gérée par le BlocListener Navigator.of(context).pop(); - Navigator.of(context).pushReplacementNamed('/dashboard'); }, onAuthError: (error) { debugPrint('❌ Erreur d\'authentification: $error'); diff --git a/unionflow-mobile-apps/lib/features/dashboard/presentation/pages/role_dashboards/visitor_dashboard.dart b/unionflow-mobile-apps/lib/features/dashboard/presentation/pages/role_dashboards/visitor_dashboard.dart index 2806773..e6e6c60 100644 --- a/unionflow-mobile-apps/lib/features/dashboard/presentation/pages/role_dashboards/visitor_dashboard.dart +++ b/unionflow-mobile-apps/lib/features/dashboard/presentation/pages/role_dashboards/visitor_dashboard.dart @@ -117,11 +117,13 @@ class VisitorDashboard extends StatelessWidget { children: [ const Icon(Icons.info_outline, color: Colors.white, size: 30), const SizedBox(width: SpacingTokens.sm), - Text( - 'Découvrez notre communauté', - style: TypographyTokens.headlineMedium.copyWith( - color: Colors.white, - fontWeight: FontWeight.bold, + Expanded( + child: Text( + 'Découvrez notre communauté', + style: TypographyTokens.headlineMedium.copyWith( + color: Colors.white, + fontWeight: FontWeight.bold, + ), ), ), ], diff --git a/unionflow-mobile-apps/lib/main.dart b/unionflow-mobile-apps/lib/main.dart index 8b9a43c..49ca2ab 100644 --- a/unionflow-mobile-apps/lib/main.dart +++ b/unionflow-mobile-apps/lib/main.dart @@ -74,22 +74,29 @@ class UnionFlowApp extends StatelessWidget { GlobalCupertinoLocalizations.delegate, ], - // Page d'accueil avec authentification - home: BlocBuilder( - builder: (context, state) { - if (state is AuthLoading) { - return const Scaffold( - body: Center( - child: CircularProgressIndicator(), - ), - ); - } else if (state is AuthAuthenticated) { - return const AdaptiveDashboardPage(); - } else { - return const LoginPage(); - } - }, - ), + // Configuration des routes + routes: { + '/': (context) => BlocBuilder( + builder: (context, state) { + if (state is AuthLoading) { + return const Scaffold( + body: Center( + child: CircularProgressIndicator(), + ), + ); + } else if (state is AuthAuthenticated) { + return const AdaptiveDashboardPage(); + } else { + return const LoginPage(); + } + }, + ), + '/dashboard': (context) => const AdaptiveDashboardPage(), + '/login': (context) => const LoginPage(), + }, + + // Page d'accueil par défaut + initialRoute: '/', // Builder global pour gérer les erreurs builder: (context, child) {