diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist index 255f5ec..c95899e 100644 --- a/ios/Runner/Info.plist +++ b/ios/Runner/Info.plist @@ -49,5 +49,7 @@ We need your location to show it on the map. NSLocationAlwaysAndWhenInUseUsageDescription We need your location to show it on the map even in background. + GIDClientID + 521527250907-3i1qe2656eojs8k9hjdi573j09i9p41m.apps.googleusercontent.com diff --git a/lib/components/profile/profile_content.dart b/lib/components/profile/profile_content.dart index 4f589c6..def1557 100644 --- a/lib/components/profile/profile_content.dart +++ b/lib/components/profile/profile_content.dart @@ -47,8 +47,9 @@ class ProfileContent extends StatelessWidget { radius: 40, backgroundColor: Theme.of(context).colorScheme.primary, child: Text( - user.prenom[0].toUpperCase() + - user.nom[0].toUpperCase(), + user.prenom.isNotEmpty + ? user.prenom[0].toUpperCase() + : 'U', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, diff --git a/lib/pages/login.dart b/lib/pages/login.dart index a2bdd9c..d26e67d 100644 --- a/lib/pages/login.dart +++ b/lib/pages/login.dart @@ -67,6 +67,57 @@ class _LoginPageState extends State { } } + Future _signInWithGoogle() async { + setState(() { + _isLoading = true; + }); + + try { + final userCredential = await _authService.signInWithGoogle(); + + if (mounted && userCredential.user != null) { + final user = userCredential.user!; + final userProvider = Provider.of(context, listen: false); + + // Récupérer les données utilisateur depuis Firestore + final userData = await userProvider.getUserData(user.uid); + + if (userData != null) { + // L'utilisateur existe déjà + userProvider.setCurrentUser(userData); + Navigator.pushReplacementNamed(context, '/home'); + } else { + // L'utilisateur n'existe pas, créer son profil + final newUserData = { + 'uid': user.uid, + 'email': user.email ?? '', + 'name': user.displayName ?? 'Utilisateur', + }; + + // Créer le profil utilisateur dans Firestore + final createdUser = await userProvider.createUser(newUserData); + + if (createdUser != null) { + userProvider.setCurrentUser(createdUser); + Navigator.pushReplacementNamed(context, '/home'); + } else { + _showErrorMessage('Erreur lors de la création du profil utilisateur'); + } + } + } + } catch (e) { + if (mounted) { + _showErrorMessage('Erreur lors de la connexion avec Google: ${e.toString()}'); + } + } finally { + if (mounted) { + setState(() { + _isLoading = false; + }); + } + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -215,12 +266,7 @@ class _LoginPageState extends State { children: [ // GOOGLE GestureDetector( - onTap: () { - // TODO: Implémenter la connexion Google - _showErrorMessage( - 'Connexion Google non implémentée', - ); - }, + onTap: _isLoading ? null : _signInWithGoogle, child: Container( width: 50, height: 50, diff --git a/lib/providers/user_provider.dart b/lib/providers/user_provider.dart index 3161b89..a7250fa 100644 --- a/lib/providers/user_provider.dart +++ b/lib/providers/user_provider.dart @@ -66,6 +66,27 @@ class UserProvider extends ChangeNotifier { } } + // Méthode pour créer un nouvel utilisateur dans Firestore + Future createUser(Map userData) async { + try { + // Structurer les données pour que tous les utilisateurs aient le même format + final userDoc = { + 'id': userData['uid'], + 'email': userData['email'] ?? '', + 'nom': '', // Nom vide pour tous les utilisateurs + 'prenom': userData['name'] ?? userData['nom'] ?? 'Utilisateur', // Nom complet dans prenom + }; + + await _firestore.collection('users').doc(userData['uid']).set(userDoc); + + // Retourner l'objet User créé + return User.fromMap({...userDoc, 'id': userData['uid']}); + } catch (e) { + print('Erreur lors de la création de l\'utilisateur: $e'); + return null; + } + } + // Initialiser l'utilisateur connecté Future initializeUser() async { firebase_auth.User? firebaseUser = _authService.currentUser; diff --git a/lib/services/auth_service.dart b/lib/services/auth_service.dart index de5d11e..21083ae 100644 --- a/lib/services/auth_service.dart +++ b/lib/services/auth_service.dart @@ -1,4 +1,6 @@ import 'package:firebase_auth/firebase_auth.dart'; +import 'package:google_sign_in/google_sign_in.dart'; +import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart'; class AuthService { final FirebaseAuth firebaseAuth = FirebaseAuth.instance; @@ -64,5 +66,39 @@ class AuthService { // Update the password await currentUser!.updatePassword(newPassword); } + + Future ensureInitialized(){ + return GoogleSignInPlatform.instance.init(const InitParameters()); + } + + Future signInWithGoogle() async { + try { + await ensureInitialized(); + final AuthenticationResults result = await GoogleSignInPlatform.instance.authenticate(const AuthenticateParameters()); + final String? idToken = result.authenticationTokens.idToken; + if (idToken == null) { + throw FirebaseAuthException( + code: 'ERROR_MISSING_GOOGLE_ID_TOKEN', + message: 'Missing Google ID Token', + ); + } else { + final OAuthCredential credential = GoogleAuthProvider.credential(idToken: idToken); + UserCredential userCredential = await firebaseAuth.signInWithCredential(credential); + + // Retourner le UserCredential au lieu de void + return userCredential; + } + + } on GoogleSignInException catch (e) { + print('Erreur lors de l\'initialisation de Google Sign-In: $e'); + rethrow; + } on FirebaseAuthException catch (e) { + print('Erreur Firebase lors de l\'initialisation de Google Sign-In: $e'); + rethrow; + } catch (e) { + print('Erreur inconnue lors de l\'initialisation de Google Sign-In: $e'); + rethrow; + } + } } \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 7b06651..8935669 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -200,6 +200,14 @@ packages: description: flutter source: sdk version: "0.0.0" + google_identity_services_web: + dependency: transitive + description: + name: google_identity_services_web + sha256: "5d187c46dc59e02646e10fe82665fc3884a9b71bc1c90c2b8b749316d33ee454" + url: "https://pub.dev" + source: hosted + version: "0.3.3+1" google_maps: dependency: transitive description: @@ -248,6 +256,46 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.14+2" + google_sign_in: + dependency: "direct main" + description: + name: google_sign_in + sha256: "521031b65853b4409b8213c0387d57edaad7e2a949ce6dea0d8b2afc9cb29763" + url: "https://pub.dev" + source: hosted + version: "7.2.0" + google_sign_in_android: + dependency: transitive + description: + name: google_sign_in_android + sha256: "7abdfa0088dc8f7d08eb3dbb1665a72bcb5b37afa256c9ec5d21e1e2d7503e5c" + url: "https://pub.dev" + source: hosted + version: "7.2.0" + google_sign_in_ios: + dependency: transitive + description: + name: google_sign_in_ios + sha256: d9d80f953a244a099a40df1ff6aadc10ee375e6a098bbd5d55be332ce26db18c + url: "https://pub.dev" + source: hosted + version: "6.2.1" + google_sign_in_platform_interface: + dependency: transitive + description: + name: google_sign_in_platform_interface + sha256: "7f59208c42b415a3cca203571128d6f84f885fead2d5b53eb65a9e27f2965bb5" + url: "https://pub.dev" + source: hosted + version: "3.1.0" + google_sign_in_web: + dependency: transitive + description: + name: google_sign_in_web + sha256: "2fc1f941e6443b2d6984f4056a727a3eaeab15d8ee99ba7125d79029be75a1da" + url: "https://pub.dev" + source: hosted + version: "1.1.0" html: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5c87b6d..6f5746d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -43,6 +43,7 @@ dependencies: firebase_core: ^4.1.1 firebase_auth: ^6.1.0 cloud_firestore: ^6.0.2 + google_sign_in: ^7.2.0 dev_dependencies: flutter_test: