2026-03-03 10:12:46 +08:00
|
|
|
import 'package:social_app/core/api/i_api_client.dart';
|
2026-02-25 14:51:21 +08:00
|
|
|
import 'models/signup_request.dart';
|
|
|
|
|
import 'models/login_request.dart';
|
|
|
|
|
import 'models/auth_response.dart';
|
|
|
|
|
|
|
|
|
|
class AuthApi {
|
2026-03-03 10:12:46 +08:00
|
|
|
final IApiClient _client;
|
2026-02-25 18:00:02 +08:00
|
|
|
static const _prefix = '/api/v1/auth';
|
2026-02-25 14:51:21 +08:00
|
|
|
|
|
|
|
|
AuthApi(this._client);
|
|
|
|
|
|
2026-02-26 14:28:58 +08:00
|
|
|
Future<VerificationCreateResponse> createVerification(
|
|
|
|
|
SignupStartRequest request,
|
|
|
|
|
) async {
|
2026-02-25 14:51:21 +08:00
|
|
|
final response = await _client.post(
|
2026-02-26 14:28:58 +08:00
|
|
|
'$_prefix/verifications',
|
2026-02-25 14:51:21 +08:00
|
|
|
data: request.toJson(),
|
|
|
|
|
);
|
2026-02-26 14:28:58 +08:00
|
|
|
return VerificationCreateResponse.fromJson(response.data);
|
2026-02-25 14:51:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-26 14:28:58 +08:00
|
|
|
Future<AuthResponse> verifyVerification(SignupVerifyRequest request) async {
|
2026-02-25 14:51:21 +08:00
|
|
|
final response = await _client.post(
|
2026-03-07 14:55:00 +08:00
|
|
|
'$_prefix/verify',
|
|
|
|
|
data: {'type': 'signup', ...request.toJson()},
|
2026-02-25 14:51:21 +08:00
|
|
|
);
|
|
|
|
|
return AuthResponse.fromJson(response.data);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 14:28:58 +08:00
|
|
|
Future<void> resendVerification(SignupResendRequest request) async {
|
2026-03-07 14:55:00 +08:00
|
|
|
await _client.post(
|
|
|
|
|
'$_prefix/resend',
|
|
|
|
|
data: {'type': 'signup', ...request.toJson()},
|
|
|
|
|
);
|
2026-02-25 14:51:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-26 14:28:58 +08:00
|
|
|
Future<AuthResponse> createSession(LoginRequest request) async {
|
2026-02-25 14:51:21 +08:00
|
|
|
final response = await _client.post(
|
2026-02-26 14:28:58 +08:00
|
|
|
'$_prefix/sessions',
|
2026-02-25 14:51:21 +08:00
|
|
|
data: request.toJson(),
|
|
|
|
|
);
|
|
|
|
|
return AuthResponse.fromJson(response.data);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 14:28:58 +08:00
|
|
|
Future<AuthResponse> refreshSession(RefreshRequest request) async {
|
2026-02-25 14:51:21 +08:00
|
|
|
final response = await _client.post(
|
2026-02-26 14:28:58 +08:00
|
|
|
'$_prefix/sessions/refresh',
|
2026-02-25 14:51:21 +08:00
|
|
|
data: request.toJson(),
|
|
|
|
|
);
|
|
|
|
|
return AuthResponse.fromJson(response.data);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 14:28:58 +08:00
|
|
|
Future<void> deleteSession(LogoutRequest request) async {
|
|
|
|
|
await _client.delete('$_prefix/sessions', data: request.toJson());
|
2026-02-25 14:51:21 +08:00
|
|
|
}
|
2026-02-27 15:22:42 +08:00
|
|
|
|
|
|
|
|
Future<void> requestPasswordReset(String email) async {
|
2026-03-07 14:55:00 +08:00
|
|
|
await _client.post(
|
|
|
|
|
'$_prefix/resend',
|
|
|
|
|
data: {'type': 'recovery', 'email': email},
|
|
|
|
|
);
|
2026-02-27 15:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> confirmPasswordReset({
|
|
|
|
|
required String email,
|
|
|
|
|
required String token,
|
|
|
|
|
required String newPassword,
|
|
|
|
|
}) async {
|
|
|
|
|
await _client.post(
|
2026-03-07 14:55:00 +08:00
|
|
|
'$_prefix/verify',
|
|
|
|
|
data: {
|
|
|
|
|
'type': 'recovery',
|
|
|
|
|
'email': email,
|
|
|
|
|
'token': token,
|
|
|
|
|
'new_password': newPassword,
|
|
|
|
|
},
|
2026-02-27 15:22:42 +08:00
|
|
|
);
|
|
|
|
|
}
|
2026-02-25 14:51:21 +08:00
|
|
|
}
|