111 lines
3.6 KiB
Dart
111 lines
3.6 KiB
Dart
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'dart:async';
|
|
|
|
import '../views/login_page.dart';
|
|
|
|
import '../providers/local_user_provider.dart';
|
|
import '../views/widgets/common/startup_splash_screen.dart';
|
|
|
|
/// Gate de démarrage qui attend la restauration Firebase Auth avant
|
|
/// d'afficher soit le contenu connecté, soit la page de connexion.
|
|
class AppStartGate extends StatelessWidget {
|
|
const AppStartGate({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Sur le web, certaines erreurs natives (ex: cookies tiers bloqués)
|
|
// peuvent faire remonter une FirebaseException sur le stream d'auth.
|
|
// Pour éviter que StreamBuilder reçoive une erreur qui casse le build
|
|
// (TypeError JS interop), on "handleError" et on transforme l'erreur
|
|
// en une valeur nulle (pas d'utilisateur) afin de garder l'app stable.
|
|
// Accès protégé à `FirebaseAuth.instance` — sur le web certaines erreurs
|
|
// d'interop JS peuvent produire des TypeError non compatibles. Nous
|
|
// attrapons toute exception lors de l'accès et fournissons un stream
|
|
// neutre (pas d'utilisateur) afin de garder l'UI stable.
|
|
late final Stream<User?> safeAuthStream;
|
|
try {
|
|
safeAuthStream = FirebaseAuth.instance
|
|
.authStateChanges()
|
|
.handleError((error, stack) {
|
|
// Log pour debug ; ne rethrow pas
|
|
debugPrint('[AppStartGate] authStateChanges error: $error');
|
|
});
|
|
} catch (e, st) {
|
|
// Sur certaines configurations web l'accès à FirebaseAuth.instance
|
|
// peut échouer au niveau JS interop. On log puis on fournit un stream
|
|
// qui émet une seule valeur nulle pour indiquer "pas d'utilisateur".
|
|
debugPrint('[AppStartGate] FirebaseAuth.instance access error: $e\n$st');
|
|
safeAuthStream = Stream<User?>.value(null);
|
|
}
|
|
|
|
return StreamBuilder<User?>(
|
|
stream: safeAuthStream,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const StartupSplashScreen();
|
|
}
|
|
|
|
if (snapshot.hasError) {
|
|
// En théorie handleError évite d'arriver ici, mais on garde
|
|
// une protection supplémentaire.
|
|
debugPrint('[AppStartGate] snapshot error: ${snapshot.error}');
|
|
return const StartupSplashScreen(message: 'Erreur de connexion');
|
|
}
|
|
|
|
if (snapshot.data != null) {
|
|
return const _AuthenticatedBootstrap();
|
|
}
|
|
|
|
return const LoginPage();
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
class _AuthenticatedBootstrap extends StatefulWidget {
|
|
const _AuthenticatedBootstrap();
|
|
|
|
@override
|
|
State<_AuthenticatedBootstrap> createState() =>
|
|
_AuthenticatedBootstrapState();
|
|
}
|
|
|
|
class _AuthenticatedBootstrapState extends State<_AuthenticatedBootstrap> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_redirectAfterAuth();
|
|
});
|
|
}
|
|
|
|
Future<void> _redirectAfterAuth() async {
|
|
final fragment = Uri.base.fragment;
|
|
|
|
if (!mounted) return;
|
|
|
|
// Charger les données utilisateur de façon non bloquante
|
|
unawaited(
|
|
context.read<LocalUserProvider>().loadUserData().catchError((e) {
|
|
debugPrint('[AppStartGate] User data bootstrap failed: $e');
|
|
}),
|
|
);
|
|
|
|
if (fragment.isNotEmpty && fragment != '/' && fragment != '/calendar') {
|
|
Navigator.of(context).pushReplacementNamed(fragment);
|
|
} else {
|
|
Navigator.of(context).pushReplacementNamed('/calendar');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const StartupSplashScreen();
|
|
}
|
|
}
|
|
|
|
|
|
|