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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -14,32 +14,42 @@ class OwnerRepository {
|
|||
|
||||
final ApiClient _apiClient;
|
||||
final WebsocketClient _wsClient;
|
||||
List<Owner>? _cachedData;
|
||||
List<Owner>? _cachedOwners;
|
||||
|
||||
Future<Result<Owner>> postOwner(
|
||||
String firstName,
|
||||
String lastName,
|
||||
String contact,
|
||||
) async {
|
||||
return Result.ok(
|
||||
Owner(firstName: firstName, lastName: lastName, contact: contact, id: 50),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Result<List<Owner>>> getOwners() async {
|
||||
if (_cachedData == null) {
|
||||
if (_cachedOwners == null) {
|
||||
final result = await _apiClient.getOwners();
|
||||
|
||||
if (result is Ok<List<Owner>>) {
|
||||
_cachedData = result.value;
|
||||
_cachedOwners = result.value;
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return Result.ok(_cachedData!);
|
||||
return Result.ok(_cachedOwners!);
|
||||
}
|
||||
}
|
||||
|
||||
Stream<Owner> liveOwners() async* {
|
||||
await for (String data in _wsClient.connect()) {
|
||||
await for (String data in await _wsClient.connect()) {
|
||||
Map<String, dynamic> decodedData = jsonDecode(
|
||||
data,
|
||||
).cast<Map<String, dynamic>>();
|
||||
Owner owner = Owner.fromJSON(decodedData);
|
||||
if (_cachedData == null) {
|
||||
getOwners();
|
||||
if (_cachedOwners == null) {
|
||||
await getOwners();
|
||||
} else {
|
||||
_cachedData!.add(owner);
|
||||
_cachedOwners!.add(owner);
|
||||
yield* Stream.value(owner);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue