feat: added authentification and redirection

This commit is contained in:
Alzalia 2025-08-08 01:03:48 +02:00
parent 1c9c5ce5fe
commit ef641d4023
24 changed files with 731 additions and 173 deletions

View file

@ -1,8 +1,61 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:seshat/config/constants.dart';
import 'package:seshat/data/services/auth_client.dart';
import 'package:seshat/domain/models/owner.dart';
import 'package:seshat/utils/command.dart';
import 'package:seshat/utils/result.dart';
typedef AuthHeaderProvider = String? Function();
class ApiClient {
ApiClient({
String? host,
int? port,
HttpClient Function()? clientFactory,
required AuthClient authClient,
}) : _authClient = authClient;
final AuthClient _authClient;
late final Command0 load;
String? token;
bool isReady = false;
FlutterSecureStorage? _secureStorage;
Future<void> _initStore() async {
_secureStorage ??= const FlutterSecureStorage(
aOptions: AndroidOptions(encryptedSharedPreferences: true),
);
}
Future<Result<List<Owner>>> getOwners() async {
return Result.ok(<Owner>[]);
final client = HttpClient();
try {
await _initStore();
final request = await client.getUrl(
Uri.parse("https://$apiBasePath/owners"),
);
final token = await _secureStorage!.read(key: "token");
debugPrint("\n\n\n\nFOUND TOKEN : $token\n\n\n\n");
// await _authHeader(request.headers);
request.headers.add(HttpHeaders.authorizationHeader, "Bearer $token");
final response = await request.close();
if (response.statusCode == 200) {
final stringData = await response.transform(Utf8Decoder()).join();
final json = jsonDecode(stringData) as List<dynamic>;
return Result.ok(
json.map((element) => Owner.fromJSON(element)).toList(),
);
} else {
return const Result.error(HttpException("Invalid response"));
}
} on Exception catch (error) {
return Result.error(error);
} finally {
client.close();
}
}
}