75 lines
1.7 KiB
Dart
75 lines
1.7 KiB
Dart
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();
|
|
}
|
|
}
|
|
|