import 'package:flutter/foundation.dart'; import 'identity_service.dart'; class SessionNotifier extends ChangeNotifier { final IdentityService _identityService; UserSession? _session; bool _isLoading = true; // 초기값을 true로 설정하여 깜빡임 방지 SessionNotifier(this._identityService); UserSession? get session => _session; bool get isGuest => _session?.isGuest ?? true; bool get isLoading => _isLoading; Future loadSession() async { _setLoading(true); try { // 1. 저장된 세션 불러오기 _session = await _identityService.getUserSession(); // 2. [Fix] 저장된 세션이 없으면 자동으로 게스트 로그인 수행 if (_session == null) { await loginGuest(); } } catch (e) { debugPrint("Session load error: $e"); // 에러 발생 시에도 게스트로 진입 시도 await loginGuest(); } finally { _setLoading(false); } } Future login(String provider) async { if (provider == 'guest') { await loginGuest(); } else { await loginSocial( provider: provider, email: "$provider@example.com", name: "User ($provider)", ); } } Future loginGuest() async { try { final userId = await _identityService.getOrCreateUser(); _session = UserSession( userId: userId, provider: 'guest', userName: '게스트', // 기본 이름 부여 ); // 게스트 정보도 세션 스토리지에 저장하여 다음 실행 시 유지 await _identityService.saveSocialLogin( userId: userId, provider: 'guest', name: '게스트' ); } catch (e) { debugPrint("Guest login failed: $e"); } notifyListeners(); } Future loginSocial({ required String provider, required String email, String? name, String? photoUrl, }) async { _setLoading(true); try { _session = await _identityService.saveSocialLogin( userId: "master-id-${email ?? provider}", email: email, name: name, photoUrl: photoUrl, provider: provider, ); notifyListeners(); } catch (e) { debugPrint("Login failed: $e"); } finally { _setLoading(false); } } Future logout() async { _setLoading(true); await _identityService.logout(); _session = null; await loginGuest(); // 로그아웃 후 다시 게스트로 전환 _setLoading(false); } void _setLoading(bool value) { _isLoading = value; notifyListeners(); } }