Add Auth service and Login Page

This commit is contained in:
2026-03-06 08:53:41 +01:00
parent 46467c457a
commit 6a25030d90
4 changed files with 280 additions and 4 deletions

View File

@@ -0,0 +1,74 @@
import 'package:flutter/material.dart';
import 'package:frontend_splatournament_manager/services/auth_service.dart';
class AuthProvider extends ChangeNotifier {
final AuthService _authService = AuthService();
bool _isLoggedIn = false;
String? _username;
int? _userId;
String? _error;
bool _isLoading = false;
bool get isLoggedIn => _isLoggedIn;
String? get username => _username;
int? get userId => _userId;
String? get error => _error;
bool get isLoading => _isLoading;
Future<bool> login(String username, String password) async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final user = await _authService.login(username, password);
_isLoggedIn = true;
_username = user['username'];
_userId = user['id'];
_isLoading = false;
notifyListeners();
return true;
} catch (e) {
_error = e.toString().replaceFirst('Exception: ', '');
_isLoading = false;
notifyListeners();
return false;
}
}
Future<bool> register(String username, String password) async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final user = await _authService.register(username, password);
_isLoggedIn = true;
_username = user['username'];
_userId = user['id'];
_isLoading = false;
notifyListeners();
return true;
} catch (e) {
_error = e.toString().replaceFirst('Exception: ', '');
_isLoading = false;
notifyListeners();
return false;
}
}
void logout() {
_isLoggedIn = false;
_username = null;
_userId = null;
_error = null;
notifyListeners();
}
void clearError() {
_error = null;
notifyListeners();
}
}