108 lines
3.1 KiB
Dart
108 lines
3.1 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:real_estate_mobile/core/constants/api_constants.dart';
|
|
import 'package:real_estate_mobile/core/network/api_client.dart';
|
|
import 'package:real_estate_mobile/core/network/api_exceptions.dart';
|
|
import 'package:real_estate_mobile/core/storage/secure_storage.dart';
|
|
import 'package:real_estate_mobile/features/auth/data/models/auth_response.dart';
|
|
import 'package:real_estate_mobile/features/auth/data/models/register_request.dart';
|
|
import 'package:real_estate_mobile/features/auth/data/models/user_model.dart';
|
|
|
|
class AuthRepository {
|
|
final Dio _dio = ApiClient.instance.dio;
|
|
|
|
Future<Map<String, dynamic>> login({
|
|
required String email,
|
|
required String password,
|
|
}) async {
|
|
try {
|
|
final response = await _dio.post(
|
|
ApiConstants.authLogin,
|
|
data: {'email': email, 'password': password},
|
|
);
|
|
|
|
final data = response.data['data'] as Map<String, dynamic>;
|
|
|
|
// Check if 2FA is required
|
|
if (data['requiresTwoFactor'] == true) {
|
|
return {
|
|
'requiresTwoFactor': true,
|
|
'tempToken': data['tempToken'],
|
|
};
|
|
}
|
|
|
|
final authResponse = AuthResponse.fromJson(data);
|
|
|
|
await SecureStorage.saveTokens(
|
|
accessToken: authResponse.accessToken,
|
|
refreshToken: authResponse.refreshToken,
|
|
);
|
|
|
|
return {
|
|
'requiresTwoFactor': false,
|
|
'user': authResponse.user,
|
|
};
|
|
} on DioException catch (e) {
|
|
if (e.error is ApiException) {
|
|
throw e.error as ApiException;
|
|
}
|
|
throw ApiException(
|
|
message: e.message ?? 'Login failed',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<UserModel> getMe() async {
|
|
try {
|
|
final response = await _dio.get(ApiConstants.authMe);
|
|
final data = response.data['data'] as Map<String, dynamic>;
|
|
return UserModel.fromJson(data);
|
|
} on DioException catch (e) {
|
|
if (e.error is ApiException) {
|
|
throw e.error as ApiException;
|
|
}
|
|
throw ApiException(
|
|
message: e.message ?? 'Failed to fetch user',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
try {
|
|
await _dio.post(ApiConstants.authLogout);
|
|
} catch (_) {
|
|
// Ignore logout API errors — still clear local tokens
|
|
} finally {
|
|
await SecureStorage.clearTokens();
|
|
}
|
|
}
|
|
|
|
Future<UserModel> register(RegisterRequest request) async {
|
|
try {
|
|
final response = await _dio.post(
|
|
ApiConstants.authRegister,
|
|
data: request.toJson(),
|
|
);
|
|
|
|
final data = response.data['data'] as Map<String, dynamic>;
|
|
final authResponse = AuthResponse.fromJson(data);
|
|
|
|
await SecureStorage.saveTokens(
|
|
accessToken: authResponse.accessToken,
|
|
refreshToken: authResponse.refreshToken,
|
|
);
|
|
|
|
return authResponse.user;
|
|
} on DioException catch (e) {
|
|
if (e.error is ApiException) {
|
|
throw e.error as ApiException;
|
|
}
|
|
throw ApiException(
|
|
message: e.message ?? 'Registration failed',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
}
|