import 'package:dio/dio.dart'; import '../../../../core/network/dio_client.dart'; import '../../../../core/security/crypto_service.dart'; class AuthRepository { final Dio dio = DioClient().dio; final CryptoService _crypto = CryptoService(); Future _ensureCryptoReady() async { if (!_crypto.isInitialized) { await _crypto.fetchPublicKey(); } } Future signup(String email, String password) async { try { await _ensureCryptoReady(); await dio.post('/auth/signup', data: { 'email': _crypto.encrypt(email), 'password': _crypto.encrypt(password), }); } on DioException catch (e) { final data = e.response?.data; final errorMsg = data is Map ? data['error'] : data; throw Exception(errorMsg ?? e.message); } } Future> login(String email, String password) async { try { await _ensureCryptoReady(); final response = await dio.post('/auth/login', data: { 'email': _crypto.encrypt(email), 'password': _crypto.encrypt(password), }); return response.data; } on DioException catch (e) { final data = e.response?.data; final errorMsg = data is Map ? data['error'] : data; throw Exception(errorMsg ?? 'Invalid email or password'); } } Future> verifyOtp(String email, String otp) async { try { final response = await dio.post('/auth/verify-otp', data: { 'email': email, 'otp': otp, }); return response.data; } on DioException catch (e) { final data = e.response?.data; final errorMsg = data is Map ? data['error'] : data; throw Exception(errorMsg ?? 'Invalid OTP'); } } Future forgotPassword(String email) async { try { await dio.post('/auth/forgot-password', data: { 'email': email, }); } on DioException catch (e) { final data = e.response?.data; final errorMsg = data is Map ? data['error'] : data; throw Exception(errorMsg ?? 'Failed to send reset code'); } } Future resetPassword(String email, String otp, String newPassword) async { try { await _ensureCryptoReady(); await dio.post('/auth/reset-password', data: { 'email': email, 'otp': otp, 'newPassword': _crypto.encrypt(newPassword), }); } on DioException catch (e) { final data = e.response?.data; final errorMsg = data is Map ? data['error'] : data; throw Exception(errorMsg ?? 'Failed to reset password'); } } }