import 'package:flutter/foundation.dart'; import '../models/user.dart'; import '../services/api_service.dart'; import '../services/auth_service.dart'; class AuthProvider with ChangeNotifier { final ApiService _apiService = ApiService(); late final AuthService _authService; User? _user; bool _isLoading = true; String? _error; AuthProvider() { _authService = AuthService(_apiService); _loadStoredUser(); } User? get user => _user; bool get isLoading => _isLoading; bool get isAuthenticated => _user != null && !_user!.isExpired; String? get error => _error; String? get token => _user?.token; ApiService get apiService => _apiService; Future _loadStoredUser() async { _isLoading = true; notifyListeners(); _user = await _authService.getStoredUser(); _isLoading = false; notifyListeners(); } Future login(String username, String password) async { _isLoading = true; _error = null; notifyListeners(); try { _user = await _authService.login(username, password); _isLoading = false; if (_user == null) { _error = 'Invalid credentials'; } notifyListeners(); return _user != null; } catch (e) { _error = 'Connection error'; _isLoading = false; notifyListeners(); return false; } } Future logout() async { await _authService.logout(); _user = null; notifyListeners(); } }