Initialer Commit: Projekt Start
This commit is contained in:
63
lib/providers/user_provider.dart
Normal file
63
lib/providers/user_provider.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../models/user.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class UserProvider extends ChangeNotifier {
|
||||
final AuthService _authService = AuthService();
|
||||
User? _user;
|
||||
bool _isLoading = false;
|
||||
|
||||
User? get user => _user;
|
||||
bool get isLoggedIn => _user != null;
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
UserProvider() {
|
||||
_loadSavedUser();
|
||||
}
|
||||
|
||||
Future<void> _loadSavedUser() async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_user = await _authService.getSavedUser();
|
||||
} catch (e) {
|
||||
print('Fehler beim Laden des Benutzers: $e');
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> login(String username, String password) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final user = await _authService.login(username, password);
|
||||
if (user != null) {
|
||||
_user = user;
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
print('Login-Fehler: $e');
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _authService.logout();
|
||||
_user = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> refreshUser() async {
|
||||
await _loadSavedUser();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user