167 lines
4.4 KiB
Dart
167 lines
4.4 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../config/api_config.dart';
|
|
import '../models/user_model.dart';
|
|
import './api_service.dart';
|
|
|
|
class AuthService with ChangeNotifier {
|
|
final FlutterSecureStorage _secureStorage = const FlutterSecureStorage();
|
|
User? _currentUser;
|
|
String? _token;
|
|
bool _isLoading = false;
|
|
bool _isInitialized = false;
|
|
final ApiService _apiService = ApiService();
|
|
|
|
User? get currentUser => _currentUser;
|
|
String? get token => _token;
|
|
bool get isLoading => _isLoading;
|
|
bool get isAuthenticated => _token != null;
|
|
bool get isInitialized => _isInitialized;
|
|
|
|
// 초기화 함수
|
|
Future<void> initialize() async {
|
|
_token = await _secureStorage.read(key: ApiConfig.tokenKey);
|
|
if (_token != null) {
|
|
await _fetchUserProfile();
|
|
}
|
|
_isInitialized = true;
|
|
notifyListeners();
|
|
}
|
|
|
|
// 로그인 함수
|
|
Future<bool> login(String username, String password) async {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
try {
|
|
final data = await _apiService.post(
|
|
ApiConfig.login,
|
|
data: {'username': username, 'password': password},
|
|
);
|
|
|
|
_token = data['token'];
|
|
_currentUser = User.fromJson(data['user']);
|
|
|
|
// 토큰 저장
|
|
await _secureStorage.write(key: ApiConfig.tokenKey, value: _token);
|
|
|
|
// API 서비스에 토큰 설정
|
|
_apiService.setToken(_token!);
|
|
|
|
// 사용자 ID와 역할 저장
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(ApiConfig.userIdKey, _currentUser!.id);
|
|
await prefs.setString(ApiConfig.userRoleKey, _currentUser!.role);
|
|
|
|
// 클럽 ID가 있으면 저장
|
|
if (_currentUser!.clubId != null) {
|
|
await prefs.setString(ApiConfig.clubIdKey, _currentUser!.clubId!);
|
|
}
|
|
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
return true;
|
|
} catch (e) {
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 로그아웃 함수
|
|
Future<void> logout() async {
|
|
_token = null;
|
|
_currentUser = null;
|
|
|
|
// 저장된 데이터 삭제
|
|
await _secureStorage.delete(key: ApiConfig.tokenKey);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove(ApiConfig.userIdKey);
|
|
await prefs.remove(ApiConfig.userRoleKey);
|
|
await prefs.remove(ApiConfig.clubIdKey);
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
// 사용자 프로필 정보 가져오기
|
|
Future<void> _fetchUserProfile() async {
|
|
if (_token == null) {
|
|
print('프로필 정보 가져오기 실패: 토큰이 없음');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
print('프로필 정보 요청 시작: ${ApiConfig.profile}');
|
|
_apiService.setToken(_token!);
|
|
final data = await _apiService.get(ApiConfig.profile);
|
|
print('프로필 API 응답: $data');
|
|
|
|
if (data != null) {
|
|
try {
|
|
_currentUser = User.fromJson(data);
|
|
print('프로필 정보 파싱 성공: ${_currentUser?.name}');
|
|
notifyListeners();
|
|
} catch (e) {
|
|
print('프로필 정보 파싱 오류: $e');
|
|
print('파싱 실패한 데이터: $data');
|
|
}
|
|
} else {
|
|
print('프로필 정보 가져오기 실패: 응답 데이터가 null');
|
|
}
|
|
} catch (e) {
|
|
print('프로필 정보 가져오기 오류: $e');
|
|
}
|
|
}
|
|
|
|
// 회원가입 함수
|
|
Future<bool> register(String name, String email, String password) async {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
try {
|
|
final data = await _apiService.post(
|
|
ApiConfig.register,
|
|
data: {'name': name, 'email': email, 'password': password},
|
|
);
|
|
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
|
|
if (data != null) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 비밀번호 재설정 이메일 전송 함수
|
|
Future<bool> sendPasswordResetEmail(String email) async {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
try {
|
|
final data = await _apiService.post(
|
|
ApiConfig.forgotPassword,
|
|
data: {'email': email},
|
|
);
|
|
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
|
|
return data != null;
|
|
} catch (e) {
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
return false;
|
|
}
|
|
}
|
|
}
|