2026-04-10 10:40:44 +08:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
|
|
|
|
|
import '../../../../core/network/api_problem.dart';
|
2026-04-02 18:39:35 +08:00
|
|
|
import '../../../../data/network/api_client.dart';
|
|
|
|
|
import '../models/session_response.dart';
|
|
|
|
|
|
|
|
|
|
class AuthApi {
|
|
|
|
|
AuthApi({required ApiClient apiClient}) : _apiClient = apiClient;
|
|
|
|
|
|
|
|
|
|
final ApiClient _apiClient;
|
|
|
|
|
|
|
|
|
|
Future<void> sendOtp({required String email}) async {
|
|
|
|
|
await _apiClient.postNoContent(
|
|
|
|
|
'/api/v1/auth/otp/send',
|
|
|
|
|
data: {'email': email},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<SessionResponse> createEmailSession({
|
|
|
|
|
required String email,
|
|
|
|
|
required String token,
|
|
|
|
|
}) async {
|
|
|
|
|
final json = await _apiClient.postJson(
|
|
|
|
|
'/api/v1/auth/email-session',
|
|
|
|
|
data: {'email': email, 'token': token},
|
|
|
|
|
);
|
|
|
|
|
return SessionResponse.fromJson(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deleteSession({required String refreshToken}) async {
|
2026-04-10 10:40:44 +08:00
|
|
|
final response = await _apiClient.rawDio.delete<Map<String, dynamic>>(
|
2026-04-02 18:39:35 +08:00
|
|
|
'/api/v1/auth/sessions',
|
|
|
|
|
data: {'refresh_token': refreshToken},
|
2026-04-10 10:40:44 +08:00
|
|
|
options: Options(
|
|
|
|
|
validateStatus: (status) => status != null && status < 500,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
final status = response.statusCode ?? 500;
|
|
|
|
|
if (status == 204 || status == 401) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final data = response.data;
|
|
|
|
|
if (data is Map<String, dynamic>) {
|
|
|
|
|
throw ApiProblem(
|
|
|
|
|
status: status,
|
|
|
|
|
title: (data['title'] as String?) ?? 'Request failed',
|
|
|
|
|
detail: (data['detail'] as String?) ?? '',
|
|
|
|
|
code: data['code'] as String?,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw ApiProblem(
|
|
|
|
|
status: status,
|
|
|
|
|
title: 'Request failed',
|
|
|
|
|
detail: 'Failed to delete session',
|
2026-04-02 18:39:35 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<SessionResponse> refreshSession({required String refreshToken}) async {
|
|
|
|
|
final json = await _apiClient.postJson(
|
|
|
|
|
'/api/v1/auth/sessions/refresh',
|
|
|
|
|
data: {'refresh_token': refreshToken},
|
|
|
|
|
);
|
|
|
|
|
return SessionResponse.fromJson(json);
|
|
|
|
|
}
|
|
|
|
|
}
|