50 lines
1.3 KiB
Dart
50 lines
1.3 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:seshat/data/services/auth_client.dart';
|
|
import 'package:seshat/utils/result.dart';
|
|
|
|
/// Repository to manage authentification
|
|
class AuthRepository extends ChangeNotifier {
|
|
AuthRepository({required AuthClient authClient}) : _authClient = authClient;
|
|
|
|
final AuthClient _authClient;
|
|
|
|
bool? _isAuthenticated;
|
|
|
|
/// Checks the validity of the token if not already checked
|
|
Future<bool> get isLoggedIn async {
|
|
if (_isAuthenticated != null) {
|
|
return _isAuthenticated!;
|
|
}
|
|
final result = await _authClient.hasValidToken();
|
|
switch (result) {
|
|
case Ok():
|
|
if (result.value) {
|
|
return true;
|
|
}
|
|
return false;
|
|
case Error():
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Logs in the user
|
|
Future<Result<void>> login(String username, String password) async {
|
|
try {
|
|
final result = await _authClient.login(username, password);
|
|
switch (result) {
|
|
case Ok():
|
|
_isAuthenticated = true;
|
|
return Result.ok(());
|
|
case Error():
|
|
return result;
|
|
}
|
|
} catch (e) {
|
|
return Result.error(Exception(e));
|
|
}
|
|
}
|
|
|
|
/// Gets the API's remote version
|
|
Future<Result<int>> getRemoteApiVersion() async {
|
|
return await _authClient.getRemoteApiVersion();
|
|
}
|
|
}
|