feat: added authentification and redirection
This commit is contained in:
parent
1c9c5ce5fe
commit
ef641d4023
24 changed files with 731 additions and 173 deletions
43
lib/data/repositories/auth_repository.dart
Normal file
43
lib/data/repositories/auth_repository.dart
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:seshat/data/services/auth_client.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
class AuthRepository extends ChangeNotifier {
|
||||
AuthRepository({required AuthClient authClient}) : _authClient = authClient;
|
||||
|
||||
final AuthClient _authClient;
|
||||
|
||||
bool? _isAuthenticated;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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.error(result.error);
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
debugPrintStack(stackTrace: stackTrace);
|
||||
return Result.error(Exception(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in a new issue