Line data Source code
1 : import 'package:flutter/foundation.dart';
2 : import 'package:flutter_secure_storage/flutter_secure_storage.dart';
3 : import 'package:shared_preferences/shared_preferences.dart';
4 :
5 : import '../config/api_config.dart';
6 : import '../models/user_model.dart';
7 : import './api_service.dart';
8 :
9 : class AuthService with ChangeNotifier {
10 : final FlutterSecureStorage _secureStorage = const FlutterSecureStorage();
11 : User? _currentUser;
12 : String? _token;
13 : bool _isLoading = false;
14 : bool _isInitialized = false;
15 : final ApiService _apiService = ApiService();
16 :
17 0 : User? get currentUser => _currentUser;
18 0 : String? get token => _token;
19 2 : bool get isLoading => _isLoading;
20 2 : bool get isAuthenticated => _token != null;
21 2 : bool get isInitialized => _isInitialized;
22 :
23 : // 초기화 함수
24 1 : Future<void> initialize() async {
25 2 : _token = await _secureStorage.read(key: ApiConfig.tokenKey);
26 0 : if (_token != null) {
27 0 : await _fetchUserProfile();
28 : }
29 0 : _isInitialized = true;
30 0 : notifyListeners();
31 : }
32 :
33 : // 로그인 함수
34 0 : Future<bool> login(String username, String password) async {
35 0 : _isLoading = true;
36 0 : notifyListeners();
37 :
38 : try {
39 0 : final data = await _apiService.post(
40 : ApiConfig.login,
41 0 : data: {'username': username, 'password': password},
42 : );
43 :
44 0 : _token = data['token'];
45 0 : _currentUser = User.fromJson(data['user']);
46 :
47 : // 토큰 저장
48 0 : await _secureStorage.write(key: ApiConfig.tokenKey, value: _token);
49 :
50 : // API 서비스에 토큰 설정
51 0 : _apiService.setToken(_token!);
52 :
53 : // 사용자 ID와 역할 저장
54 0 : final prefs = await SharedPreferences.getInstance();
55 0 : await prefs.setString(ApiConfig.userIdKey, _currentUser!.id);
56 0 : await prefs.setString(ApiConfig.userRoleKey, _currentUser!.role);
57 :
58 : // 클럽 ID가 있으면 저장
59 0 : if (_currentUser!.clubId != null) {
60 0 : await prefs.setString(ApiConfig.clubIdKey, _currentUser!.clubId!);
61 : }
62 :
63 0 : _isLoading = false;
64 0 : notifyListeners();
65 : return true;
66 : } catch (e) {
67 0 : _isLoading = false;
68 0 : notifyListeners();
69 : return false;
70 : }
71 : }
72 :
73 : // 로그아웃 함수
74 0 : Future<void> logout() async {
75 0 : _token = null;
76 0 : _currentUser = null;
77 :
78 : // 저장된 데이터 삭제
79 0 : await _secureStorage.delete(key: ApiConfig.tokenKey);
80 :
81 0 : final prefs = await SharedPreferences.getInstance();
82 0 : await prefs.remove(ApiConfig.userIdKey);
83 0 : await prefs.remove(ApiConfig.userRoleKey);
84 0 : await prefs.remove(ApiConfig.clubIdKey);
85 :
86 0 : notifyListeners();
87 : }
88 :
89 : // 사용자 프로필 정보 가져오기
90 0 : Future<void> _fetchUserProfile() async {
91 0 : if (_token == null) {
92 0 : print('프로필 정보 가져오기 실패: 토큰이 없음');
93 : return;
94 : }
95 :
96 : try {
97 0 : print('프로필 정보 요청 시작: ${ApiConfig.profile}');
98 0 : _apiService.setToken(_token!);
99 0 : final data = await _apiService.get(ApiConfig.profile);
100 0 : print('프로필 API 응답: $data');
101 :
102 : if (data != null) {
103 : try {
104 0 : _currentUser = User.fromJson(data);
105 0 : print('프로필 정보 파싱 성공: ${_currentUser?.name}');
106 0 : notifyListeners();
107 : } catch (e) {
108 0 : print('프로필 정보 파싱 오류: $e');
109 0 : print('파싱 실패한 데이터: $data');
110 : }
111 : } else {
112 0 : print('프로필 정보 가져오기 실패: 응답 데이터가 null');
113 : }
114 : } catch (e) {
115 0 : print('프로필 정보 가져오기 오류: $e');
116 : }
117 : }
118 :
119 : // 회원가입 함수
120 0 : Future<bool> register(String name, String email, String password) async {
121 0 : _isLoading = true;
122 0 : notifyListeners();
123 :
124 : try {
125 0 : final data = await _apiService.post(
126 : ApiConfig.register,
127 0 : data: {'name': name, 'email': email, 'password': password},
128 : );
129 :
130 0 : _isLoading = false;
131 0 : notifyListeners();
132 :
133 : if (data != null) {
134 : return true;
135 : } else {
136 : return false;
137 : }
138 : } catch (e) {
139 0 : _isLoading = false;
140 0 : notifyListeners();
141 : return false;
142 : }
143 : }
144 :
145 : // 비밀번호 재설정 이메일 전송 함수
146 0 : Future<bool> sendPasswordResetEmail(String email) async {
147 0 : _isLoading = true;
148 0 : notifyListeners();
149 :
150 : try {
151 0 : final data = await _apiService.post(
152 : ApiConfig.forgotPassword,
153 0 : data: {'email': email},
154 : );
155 :
156 0 : _isLoading = false;
157 0 : notifyListeners();
158 :
159 : return data != null;
160 : } catch (e) {
161 0 : _isLoading = false;
162 0 : notifyListeners();
163 : return false;
164 : }
165 : }
166 : }
|