Performed first authentication
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
2025-07-02 22:14:52 +02:00
parent c8fa4552bb
commit ff97fb69f7
12 changed files with 227 additions and 34 deletions

View File

@ -1,4 +0,0 @@
/// Account API
class AccountAPI {
}

View File

@ -1,7 +1,72 @@
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:dio/dio.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:moneymgr_mobile/services/api/api_token.dart';
import 'package:moneymgr_mobile/utils/string_utils.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
/// Client API
@riverpod
class ClientAPI {
part 'api_client.g.dart';
}
/// API token header
const apiTokenHeader = "X-Auth-Token";
/// Client API
class ApiClient {
final ApiToken token;
final Dio client;
ApiClient({required this.token})
: client = Dio(BaseOptions(baseUrl: token.apiUrl));
/// Get Dio instance
Future<Response<T>> execute<T>(String uri, {String method = "GET"}) async {
return client.request(
uri,
options: Options(
method: method,
headers: {apiTokenHeader: _genJWT(method, uri)},
),
);
}
/// Generate authentication JWT
String _genJWT(String method, String uri) {
final jwt = JWT(
{"nonce": getRandomString(15), "met": method, "uri": uri},
header: {"kid": token.tokenId.toString()},
);
return jwt.sign(
SecretKey(token.tokenValue),
algorithm: JWTAlgorithm.HS256,
expiresIn: Duration(minutes: 15),
);
}
}
/// An API service that handles authentication and exposes an [ApiClient].
///
/// Every API call coming from UI should watch/read this provider instead of
/// instantiating the [ApiClient] itself. When being watched, it will force any
/// data provider (provider that fetches data) to refetch when the
/// authentication state changes.
///
/// The API client is kept alive to follow dio's recommendation to use the same
/// client instance for the entire app.
@riverpod
ApiClient apiService(Ref ref) {
/*final token = ref.watch(currentAuthStateProvider);
final ApiClient client;
const mock = bool.fromEnvironment('MOCK_API', defaultValue: false);
client = switch (mock) {
true =>
token != null ? MockedApiClient.withToken(token) : MockedApiClient(),
false => token != null ? ApiClient.withToken(token) : ApiClient(),
};
ref.keepAlive();
return client;*/
throw Exception("TODO"); // TODO
}

View File

@ -8,7 +8,7 @@ abstract class ApiToken with _$ApiToken {
const factory ApiToken({
required String apiUrl,
required int tokenId,
required String tokenValue
required String tokenValue,
}) = _ApiToken;
factory ApiToken.fromJson(Map<String, dynamic> json) =>

View File

@ -0,0 +1,30 @@
// ignore_for_file: non_constant_identifier_names
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:moneymgr_mobile/services/api/api_client.dart';
part 'auth_api.g.dart';
part 'auth_api.freezed.dart';
@freezed
abstract class AuthInfo with _$AuthInfo {
const factory AuthInfo({
required int id,
required String mail,
required String name,
required int time_create,
required int time_update,
}) = _AuthInfo;
factory AuthInfo.fromJson(Map<String, dynamic> json) =>
_$AuthInfoFromJson(json);
}
/// Auth API
extension AuthApi on ApiClient {
/// Get authentication information
Future<AuthInfo> authInfo() async {
final response = await execute("/auth/info", method: "GET");
return AuthInfo.fromJson(response.data);
}
}