diff --git a/lib/main.dart b/lib/main.dart index 1b9a7bb..4f14f7a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,20 +3,28 @@ import 'package:provider/provider.dart'; import 'core/theme_manager.dart'; import 'logic/game_controller.dart'; import 'ui/home/home_screen.dart'; -import 'services/storage_service.dart'; // <-- Importiamo il servizio +import 'services/storage_service.dart'; import 'package:firebase_core/firebase_core.dart'; +import 'package:firebase_auth/firebase_auth.dart'; // <-- NUOVO IMPORT import 'firebase_options.dart'; void main() async { // Assicuriamoci che i motori di Flutter siano pronti WidgetsFlutterBinding.ensureInitialized(); - // 1. Accendiamo Firebase! (Questo ti era sfuggito) + // 1. Accendiamo Firebase! await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); - // 2. Accendiamo la Memoria Locale! + // 2. Login Silenzioso (Crea la nostra "Identità Sicura" per Firebase) + try { + await FirebaseAuth.instance.signInAnonymously(); + } catch (e) { + debugPrint("Errore Auth: $e"); + } + + // 3. Accendiamo la Memoria Locale! await StorageService.instance.init(); runApp( diff --git a/lib/services/storage_service.dart b/lib/services/storage_service.dart index f53c2e7..84d3db6 100644 --- a/lib/services/storage_service.dart +++ b/lib/services/storage_service.dart @@ -6,6 +6,8 @@ import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import '../core/app_colors.dart'; +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flutter/foundation.dart'; class StorageService { static final StorageService instance = StorageService._internal(); @@ -59,15 +61,22 @@ class StorageService { Future syncLeaderboard() async { if (playerName.isNotEmpty) { try { - await FirebaseFirestore.instance.collection('leaderboard').doc(playerName).set({ - 'name': playerName, - 'xp': totalXP, - 'level': playerLevel, - 'wins': wins, - 'lastActive': FieldValue.serverTimestamp(), - }, SetOptions(merge: true)); + // Recuperiamo il nostro ID segreto e univoco appena creato + final user = FirebaseAuth.instance.currentUser; + + if (user != null) { + // Usiamo user.uid come nome del documento, NON più il playerName! + await FirebaseFirestore.instance.collection('leaderboard').doc(user.uid).set({ + 'name': playerName, // Il nome rimane dentro per mostrarlo nella lista + 'xp': totalXP, + 'level': playerLevel, + 'wins': wins, + 'lastActive': FieldValue.serverTimestamp(), + }, SetOptions(merge: true)); + } } catch(e) { // Ignoriamo gli errori se manca la rete, si sincronizzerà dopo + debugPrint("Errore sinc. classifica: $e"); } } } diff --git a/lib/ui/home/home_screen.dart b/lib/ui/home/home_screen.dart index d34bb92..71ca543 100644 --- a/lib/ui/home/home_screen.dart +++ b/lib/ui/home/home_screen.dart @@ -20,6 +20,7 @@ import '../settings/settings_screen.dart'; import '../../services/storage_service.dart'; import '../multiplayer/lobby_screen.dart'; import 'history_screen.dart'; +import 'package:firebase_auth/firebase_auth.dart'; TextStyle _getTextStyle(AppThemeType themeType, TextStyle baseStyle) { if (themeType == AppThemeType.doodle) { @@ -906,10 +907,12 @@ class _HomeScreenState extends State with WidgetsBindingObserver { final docs = snapshot.data!.docs; return ListView.builder( physics: const BouncingScrollPhysics(), - itemCount: docs.length, itemBuilder: (context, index) { var data = docs[index].data() as Map; - bool isMe = data['name'] == StorageService.instance.playerName; + + // Ora controlliamo se l'ID del documento su Firebase è uguale al nostro ID segreto! + String? myUid = FirebaseAuth.instance.currentUser?.uid; + bool isMe = docs[index].id == myUid; return Container( margin: const EdgeInsets.only(bottom: 8), diff --git a/macos/.DS_Store b/macos/.DS_Store index 80dd696..5021d47 100644 Binary files a/macos/.DS_Store and b/macos/.DS_Store differ diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 9681093..be5524b 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -8,6 +8,7 @@ import Foundation import app_links import audioplayers_darwin import cloud_firestore +import firebase_auth import firebase_core import share_plus import shared_preferences_foundation @@ -16,6 +17,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { AppLinksMacosPlugin.register(with: registry.registrar(forPlugin: "AppLinksMacosPlugin")) AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin")) FLTFirebaseFirestorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseFirestorePlugin")) + FLTFirebaseAuthPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAuthPlugin")) FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin")) SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) diff --git a/macos/Podfile.lock b/macos/Podfile.lock index 7fb6e6d..7bbc13d 100644 --- a/macos/Podfile.lock +++ b/macos/Podfile.lock @@ -1203,15 +1203,33 @@ PODS: - Firebase/Firestore (~> 12.8.0) - firebase_core - FlutterMacOS + - Firebase/Auth (12.8.0): + - Firebase/CoreOnly + - FirebaseAuth (~> 12.8.0) - Firebase/CoreOnly (12.8.0): - FirebaseCore (~> 12.8.0) - Firebase/Firestore (12.8.0): - Firebase/CoreOnly - FirebaseFirestore (~> 12.8.0) + - firebase_auth (6.1.4): + - Firebase/Auth (~> 12.8.0) + - Firebase/CoreOnly (~> 12.8.0) + - firebase_core + - FlutterMacOS - firebase_core (4.4.0): - Firebase/CoreOnly (~> 12.8.0) - FlutterMacOS - FirebaseAppCheckInterop (12.8.0) + - FirebaseAuth (12.8.0): + - FirebaseAppCheckInterop (~> 12.8.0) + - FirebaseAuthInterop (~> 12.8.0) + - FirebaseCore (~> 12.8.0) + - FirebaseCoreExtension (~> 12.8.0) + - GoogleUtilities/AppDelegateSwizzler (~> 8.1) + - GoogleUtilities/Environment (~> 8.1) + - GTMSessionFetcher/Core (< 6.0, >= 3.4) + - RecaptchaInterop (~> 101.0) + - FirebaseAuthInterop (12.8.0) - FirebaseCore (12.8.0): - FirebaseCoreInternal (~> 12.8.0) - GoogleUtilities/Environment (~> 8.1) @@ -1242,14 +1260,27 @@ PODS: - nanopb (~> 3.30910.0) - FirebaseSharedSwift (12.8.0) - FlutterMacOS (1.0.0) + - GoogleUtilities/AppDelegateSwizzler (8.1.0): + - GoogleUtilities/Environment + - GoogleUtilities/Logger + - GoogleUtilities/Network + - GoogleUtilities/Privacy - GoogleUtilities/Environment (8.1.0): - GoogleUtilities/Privacy - GoogleUtilities/Logger (8.1.0): - GoogleUtilities/Environment - GoogleUtilities/Privacy + - GoogleUtilities/Network (8.1.0): + - GoogleUtilities/Logger + - "GoogleUtilities/NSData+zlib" + - GoogleUtilities/Privacy + - GoogleUtilities/Reachability - "GoogleUtilities/NSData+zlib (8.1.0)": - GoogleUtilities/Privacy - GoogleUtilities/Privacy (8.1.0) + - GoogleUtilities/Reachability (8.1.0): + - GoogleUtilities/Logger + - GoogleUtilities/Privacy - "gRPC-C++ (1.69.0)": - "gRPC-C++/Implementation (= 1.69.0)" - "gRPC-C++/Interface (= 1.69.0)" @@ -1342,6 +1373,7 @@ PODS: - gRPC-Core/Privacy (= 1.69.0) - gRPC-Core/Interface (1.69.0) - gRPC-Core/Privacy (1.69.0) + - GTMSessionFetcher/Core (5.1.0) - leveldb-library (1.22.6) - nanopb (3.30910.0): - nanopb/decode (= 3.30910.0) @@ -1358,6 +1390,7 @@ DEPENDENCIES: - app_links (from `Flutter/ephemeral/.symlinks/plugins/app_links/macos`) - audioplayers_darwin (from `Flutter/ephemeral/.symlinks/plugins/audioplayers_darwin/macos`) - cloud_firestore (from `Flutter/ephemeral/.symlinks/plugins/cloud_firestore/macos`) + - firebase_auth (from `Flutter/ephemeral/.symlinks/plugins/firebase_auth/macos`) - firebase_core (from `Flutter/ephemeral/.symlinks/plugins/firebase_core/macos`) - FlutterMacOS (from `Flutter/ephemeral`) - share_plus (from `Flutter/ephemeral/.symlinks/plugins/share_plus/macos`) @@ -1369,6 +1402,8 @@ SPEC REPOS: - BoringSSL-GRPC - Firebase - FirebaseAppCheckInterop + - FirebaseAuth + - FirebaseAuthInterop - FirebaseCore - FirebaseCoreExtension - FirebaseCoreInternal @@ -1378,6 +1413,7 @@ SPEC REPOS: - GoogleUtilities - "gRPC-C++" - gRPC-Core + - GTMSessionFetcher - leveldb-library - nanopb @@ -1388,6 +1424,8 @@ EXTERNAL SOURCES: :path: Flutter/ephemeral/.symlinks/plugins/audioplayers_darwin/macos cloud_firestore: :path: Flutter/ephemeral/.symlinks/plugins/cloud_firestore/macos + firebase_auth: + :path: Flutter/ephemeral/.symlinks/plugins/firebase_auth/macos firebase_core: :path: Flutter/ephemeral/.symlinks/plugins/firebase_core/macos FlutterMacOS: @@ -1404,8 +1442,11 @@ SPEC CHECKSUMS: BoringSSL-GRPC: dded2a44897e45f28f08ae87a55ee4bcd19bc508 cloud_firestore: 71947b640bd24f6f849d9d185e5d0a619fa6b93b Firebase: 9a58fdbc9d8655ed7b79a19cf9690bb007d3d46d + firebase_auth: 2c2438e41f061c03bd67dcb045dfd7bc843b5f52 firebase_core: b1697fb64ff2b9ca16baaa821205f8b0c058e5d2 FirebaseAppCheckInterop: ba3dc604a89815379e61ec2365101608d365cf7d + FirebaseAuth: 4c289b1a43f5955283244a55cf6bd616de344be5 + FirebaseAuthInterop: 95363fe96493cb4f106656666a0768b420cba090 FirebaseCore: 0dbad74bda10b8fb9ca34ad8f375fb9dd3ebef7c FirebaseCoreExtension: 6605938d51f765d8b18bfcafd2085276a252bee2 FirebaseCoreInternal: fe5fa466aeb314787093a7dce9f0beeaad5a2a21 @@ -1416,6 +1457,7 @@ SPEC CHECKSUMS: GoogleUtilities: 00c88b9a86066ef77f0da2fab05f65d7768ed8e1 "gRPC-C++": cc207623316fb041a7a3e774c252cf68a058b9e8 gRPC-Core: 860978b7db482de8b4f5e10677216309b5ff6330 + GTMSessionFetcher: b8ab00db932816e14b0a0664a08cb73dda6d164b leveldb-library: cc8b8f8e013647a295ad3f8cd2ddf49a6f19be19 nanopb: fad817b59e0457d11a5dfbde799381cd727c1275 share_plus: 510bf0af1a42cd602274b4629920c9649c52f4cc diff --git a/macos/Runner/DebugProfile.entitlements b/macos/Runner/DebugProfile.entitlements index 9804039..1fbcb4e 100644 --- a/macos/Runner/DebugProfile.entitlements +++ b/macos/Runner/DebugProfile.entitlements @@ -2,13 +2,15 @@ - com.apple.security.app-sandbox - - com.apple.security.cs.allow-jit - - com.apple.security.network.client - - com.apple.security.network.server - + com.apple.security.app-sandbox + + com.apple.security.cs.allow-jit + + com.apple.security.network.client + + com.apple.security.network.server + + keychain-access-groups + - \ No newline at end of file + diff --git a/macos/Runner/Release.entitlements b/macos/Runner/Release.entitlements index b79f6de..9c17e2f 100644 --- a/macos/Runner/Release.entitlements +++ b/macos/Runner/Release.entitlements @@ -8,5 +8,7 @@ com.apple.security.network.client + keychain-access-groups + diff --git a/pubspec.lock b/pubspec.lock index dee2a86..5a4b50d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -249,6 +249,30 @@ packages: url: "https://pub.dev" source: hosted version: "7.0.1" + firebase_auth: + dependency: "direct main" + description: + name: firebase_auth + sha256: b20d1540460814c5984474c1e9dd833bdbcff6ecd8d6ad86cc9da8cfd581c172 + url: "https://pub.dev" + source: hosted + version: "6.1.4" + firebase_auth_platform_interface: + dependency: transitive + description: + name: firebase_auth_platform_interface + sha256: fd0225320b6bbc92460c86352d16b60aea15f9ef88292774cca97b0522ea9f72 + url: "https://pub.dev" + source: hosted + version: "8.1.6" + firebase_auth_web: + dependency: transitive + description: + name: firebase_auth_web + sha256: be7dccb263b89fbda2a564de9d8193118196e8481ffb937222a025cdfdf82c40 + url: "https://pub.dev" + source: hosted + version: "6.1.2" firebase_core: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 542892e..7f00324 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,6 +18,7 @@ dependencies: flutter_localizations: # Il sistema multilingua ufficiale sdk: flutter firebase_core: ^4.4.0 + firebase_auth: ^6.1.4 # <--- NUOVO: LA CORAZZA DI SICUREZZA! cloud_firestore: ^6.1.2 share_plus: ^12.0.1 app_links: ^7.0.0