53 lines
1.1 KiB
Dart
53 lines
1.1 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import '../services/auth_service.dart';
|
|
|
|
class AuthProvider with ChangeNotifier {
|
|
final AuthService _authService = AuthService();
|
|
|
|
bool _isLoading = false;
|
|
String? _error;
|
|
|
|
bool get isLoading => _isLoading;
|
|
bool get isAuthenticated => _authService.isAuthenticated;
|
|
String? get token => _authService.token;
|
|
String? get username => _authService.username;
|
|
String? get error => _error;
|
|
|
|
Future<void> init() async {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
await _authService.loadStoredAuth();
|
|
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<bool> login(String username, String password) async {
|
|
_isLoading = true;
|
|
_error = null;
|
|
notifyListeners();
|
|
|
|
final success = await _authService.login(username, password);
|
|
|
|
if (!success) {
|
|
_error = _authService.lastError ?? 'Login failed';
|
|
}
|
|
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
|
|
return success;
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
await _authService.logout();
|
|
notifyListeners();
|
|
}
|
|
|
|
void clearError() {
|
|
_error = null;
|
|
notifyListeners();
|
|
}
|
|
}
|