This commit is contained in:
Phuoc Nguyen
2025-10-10 17:51:31 +07:00
parent 10ccd0300d
commit 77440ac957
7 changed files with 300 additions and 11 deletions

View File

@@ -31,19 +31,33 @@ class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
@override
Future<AuthResponseModel> login(LoginDto loginDto) async {
try {
print('📡 DataSource: Calling login API...');
final response = await dioClient.post(
ApiConstants.login,
data: loginDto.toJson(),
);
print('📡 DataSource: Status=${response.statusCode}');
print('📡 DataSource: Response data keys=${response.data.keys.toList()}');
if (response.statusCode == ApiConstants.statusOk) {
return AuthResponseModel.fromJson(response.data);
// API returns nested structure: {success, data: {access_token, user}, message}
// Extract the 'data' object
final responseData = response.data['data'] as Map<String, dynamic>;
print('📡 DataSource: Extracted data object with keys=${responseData.keys.toList()}');
final authResponseModel = AuthResponseModel.fromJson(responseData);
print('📡 DataSource: Parsed successfully, token length=${authResponseModel.accessToken.length}');
return authResponseModel;
} else {
throw ServerException('Login failed with status: ${response.statusCode}');
}
} on DioException catch (e) {
print('❌ DataSource: DioException - ${e.message}');
throw _handleDioError(e);
} catch (e) {
} catch (e, stackTrace) {
print('❌ DataSource: Unexpected error - $e');
print('Stack trace: $stackTrace');
throw ServerException('Unexpected error during login: $e');
}
}
@@ -56,8 +70,12 @@ class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
data: registerDto.toJson(),
);
if (response.statusCode == ApiConstants.statusCreated) {
return AuthResponseModel.fromJson(response.data);
if (response.statusCode == ApiConstants.statusCreated ||
response.statusCode == ApiConstants.statusOk) {
// API returns nested structure: {success, data: {access_token, user}, message}
// Extract the 'data' object
final responseData = response.data['data'] as Map<String, dynamic>;
return AuthResponseModel.fromJson(responseData);
} else {
throw ServerException('Registration failed with status: ${response.statusCode}');
}
@@ -74,7 +92,12 @@ class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
final response = await dioClient.get(ApiConstants.profile);
if (response.statusCode == ApiConstants.statusOk) {
return UserModel.fromJson(response.data);
// API might return nested structure: {success, data: user, message}
// Check if response has 'data' key
final userData = response.data['data'] != null
? response.data['data'] as Map<String, dynamic>
: response.data as Map<String, dynamic>;
return UserModel.fromJson(userData);
} else {
throw ServerException('Get profile failed with status: ${response.statusCode}');
}
@@ -91,7 +114,10 @@ class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
final response = await dioClient.post(ApiConstants.refreshToken);
if (response.statusCode == ApiConstants.statusOk) {
return AuthResponseModel.fromJson(response.data);
// API returns nested structure: {success, data: {access_token, user}, message}
// Extract the 'data' object
final responseData = response.data['data'] as Map<String, dynamic>;
return AuthResponseModel.fromJson(responseData);
} else {
throw ServerException('Token refresh failed with status: ${response.statusCode}');
}