import 'package:flutter/material.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:sign_in_with_apple/sign_in_with_apple.dart'; import 'identity_service.dart'; import 'puzzle_service.dart'; class SessionNotifier with ChangeNotifier { final IdentityService _identityService = IdentityService(); final PuzzleService _puzzleService = PuzzleService(); UserSession? _session; UserSession? get session => _session; bool get isLoading => _session == null; bool get isGuest => _session?.isGuest ?? true; // ๐Ÿ”ฝ [์ˆ˜์ •] 'GoogleSignIn()' ์ƒ์„ฑ์ž ๋Œ€์‹  '.instance' ์‹ฑ๊ธ€ํ†ค ์‚ฌ์šฉ final GoogleSignIn _googleSignIn = GoogleSignIn.instance; SessionNotifier() { loadSession(); } /// ์•ฑ ์‹œ์ž‘ ์‹œ ์ €์žฅ๋œ ์„ธ์…˜ ๋กœ๋“œ Future loadSession() async { _session = await _identityService.getUserSession(); notifyListeners(); } /// (๋ฐฑ์—”๋“œ ์—ฐ๋™ ํ›„) ๋กœ๊ทธ์ธ/๊ณ„์ • ์—ฐ๊ฒฐ Future login(String provider) async { if (isLoading) return; final guestUserId = _session!.userId; // ํ˜„์žฌ ๊ฒŒ์ŠคํŠธ ID String? idToken; String? email; String? userName; try { if (provider == 'google') { // ๐Ÿ”ฝ [์ˆ˜์ •] 'signIn()' ๋ฉ”์„œ๋“œ ๋Œ€์‹  'authenticate()' ์‚ฌ์šฉ final GoogleSignInAccount? googleUser = await _googleSignIn.authenticate(); if (googleUser == null) return; // ์œ ์ €๊ฐ€ ์ทจ์†Œ final GoogleSignInAuthentication googleAuth = googleUser.authentication; idToken = googleAuth.idToken; // ๐Ÿ‘ˆ [ํ•ต์‹ฌ] ์ด ํ† ํฐ์„ ๋ฐฑ์—”๋“œ๋กœ ์ „์†ก email = googleUser.email; userName = googleUser.displayName; } else if (provider == 'apple') { final credential = await SignInWithApple.getAppleIDCredential( scopes: [ AppleIDAuthorizationScopes.email, AppleIDAuthorizationScopes.fullName ], ); idToken = credential.identityToken; // ๐Ÿ‘ˆ [ํ•ต์‹ฌ] ์ด ํ† ํฐ์„ ๋ฐฑ์—”๋“œ๋กœ ์ „์†ก email = credential.email; userName = "${credential.givenName ?? ''} ${credential.familyName ?? ''}".trim(); } if (idToken == null) { throw Exception("$provider ๋กœ๊ทธ์ธ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."); } // [TODO] ๋ฐฑ์—”๋“œ์— 'mergeAccount(guestUserId, idToken, provider)' API ํ˜ธ์ถœ // ๋ฐฑ์—”๋“œ๋Š” ์ด idToken์„ ๊ฒ€์ฆํ•˜๊ณ , guestUserId์˜ ๋ฐ์ดํ„ฐ๋ฅผ // ์†Œ์…œ ๊ณ„์ •์˜ ๋งˆ์Šคํ„ฐ ID๋กœ ๋ณ‘ํ•ฉ(merge)ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. // --- ๋ฐฑ์—”๋“œ ์‘๋‹ต (์ž„์‹œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜) --- // final backendResponse = await _puzzleService.mergeAccount(guestUserId, idToken, provider); // _session = await _identityService.saveSocialLogin( // newUserId: backendResponse.userId, // newUserName: backendResponse.userName, // newEmail: backendResponse.email, // provider: provider // ); // [์ž„์‹œ] ๋ฐฑ์—”๋“œ ์—†์œผ๋ฏ€๋กœ, ํด๋ผ์ด์–ธํŠธ ์ •๋ณด๋กœ ๊ฐ•์ œ ์ €์žฅ (ํ…Œ์ŠคํŠธ์šฉ) _session = await _identityService.saveSocialLogin( newUserId: "master-id-${email ?? provider}", // (์ž„์‹œ) newUserName: userName ?? "Social User", newEmail: email ?? "No Email", provider: provider ); // --- ์ž„์‹œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ --- notifyListeners(); } catch (e) { debugPrint("$provider ๋กœ๊ทธ์ธ ์˜ค๋ฅ˜: $e"); // [TODO] ์œ ์ €์—๊ฒŒ "๋กœ๊ทธ์ธ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค." ์Šค๋‚ต๋ฐ” ํ‘œ์‹œ } } /// ๋กœ๊ทธ์•„์›ƒ Future logout() async { await _googleSignIn.signOut(); _session = await _identityService.logout(); notifyListeners(); } }