Files
musadaq-saas/musadaq-app/lib/features/auth/controllers/auth_controller.dart
2026-05-13 22:58:30 +03:00

188 lines
6.5 KiB
Dart

import 'package:get/get.dart';
import 'package:dio/dio.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'dart:io';
import '../../../core/network/dio_client.dart';
import '../../../core/storage/secure_storage.dart';
import '../../../app/routes/app_pages.dart';
import '../../../core/utils/logger.dart';
import '../../../core/utils/app_snackbar.dart';
import '../../../core/services/push_notification_service.dart';
class AuthController extends GetxController {
final Dio _dio = DioClient().client;
final SecureStorage _storage = SecureStorage();
var isLoading = false.obs;
var phone = ''.obs;
Future<void> requestOtp(String phoneNumber) async {
try {
if (phoneNumber.trim().isEmpty) {
AppSnackbar.showError('خطأ', 'الرجاء إدخال رقم الهاتف أولاً');
return;
}
isLoading.value = true;
// Normalize phone number
String normalizedPhone = phoneNumber.replaceAll(RegExp(r'[^0-9+]'), '');
if (normalizedPhone.startsWith('+')) {
normalizedPhone = normalizedPhone.substring(1);
}
if (normalizedPhone.startsWith('07')) {
normalizedPhone = '962' + normalizedPhone.substring(1);
} else if (normalizedPhone.startsWith('7')) {
normalizedPhone = '962' + normalizedPhone;
}
phone.value = normalizedPhone;
final response = await _dio.post('auth/mobile/request-otp', data: {
'phone': normalizedPhone,
});
if (response.statusCode == 200) {
AppLogger.print('OTP Request Success: ${response.data}');
AppSnackbar.showSuccess('نجاح', 'تم إرسال رمز التحقق بنجاح');
Get.toNamed(AppRoutes.OTP_VERIFY);
}
} on DioException catch (e, stackTrace) {
String errorMessage = 'فشل الاتصال بالخادم';
if (e.response?.data != null && e.response?.data is Map) {
errorMessage = e.response?.data['message'] ?? errorMessage;
}
AppSnackbar.showError('خطأ', errorMessage);
} finally {
isLoading.value = false;
}
}
Future<void> verifyOtp(String otp) async {
try {
isLoading.value = true;
// Get device info
final deviceInfo = DeviceInfoPlugin();
String deviceId = '';
String deviceName = '';
if (Platform.isAndroid) {
final androidInfo = await deviceInfo.androidInfo;
deviceId = androidInfo.id;
deviceName = androidInfo.model;
} else if (Platform.isIOS) {
final iosInfo = await deviceInfo.iosInfo;
deviceId = iosInfo.identifierForVendor ?? 'unknown_ios';
deviceName = iosInfo.name;
}
// Get FCM token for notifications
final pushToken = await PushNotificationService.getToken();
final response = await _dio.post('auth/mobile/verify-otp', data: {
'phone': phone.value,
'otp': otp,
'device_id': deviceId,
'device_name': deviceName,
'platform': Platform.operatingSystem,
'app_version': '1.0.0',
'push_token': pushToken,
});
if (response.statusCode == 200) {
AppLogger.print('OTP Verify Success. Tokens received.');
final data = response.data['data'];
// Save secure data
await _storage.saveToken(data['access_token']);
await _storage.saveDeviceSecret(data['device_secret']);
if (data['user']['email'] != null) {
await _storage.saveEmail(data['user']['email']);
}
AppSnackbar.showSuccess('مرحباً بك', 'تم تسجيل الدخول بنجاح');
// Navigate to Biometric Setup (unless it's the reviewer)
if (data['user']['email'] == 'reviewer@musadaq.jo') {
Get.offAllNamed(AppRoutes.MAIN);
} else {
Get.offAllNamed(AppRoutes.BIOMETRIC_SETUP);
}
}
} on DioException catch (e, stackTrace) {
AppLogger.error('OTP Verify Failed', e.response?.data, stackTrace);
AppSnackbar.showError('خطأ', e.response?.data['message'] ?? 'رمز التحقق غير صحيح');
} finally {
isLoading.value = false;
}
}
Future<void> loginWithEmail(String email, String password) async {
try {
if (email.trim().isEmpty || password.trim().isEmpty) {
AppSnackbar.showError('خطأ', 'الرجاء إدخال البريد الإلكتروني وكلمة المرور');
return;
}
isLoading.value = true;
// Get device info
final deviceInfo = DeviceInfoPlugin();
String deviceId = '';
String deviceName = '';
if (Platform.isAndroid) {
final androidInfo = await deviceInfo.androidInfo;
deviceId = androidInfo.id;
deviceName = androidInfo.model;
} else if (Platform.isIOS) {
final iosInfo = await deviceInfo.iosInfo;
deviceId = iosInfo.identifierForVendor ?? 'unknown_ios';
deviceName = iosInfo.name;
}
final response = await _dio.post('auth/login', data: {
'email': email,
'password': password,
'device_id': deviceId,
'device_name': deviceName,
'platform': Platform.operatingSystem,
'app_version': '1.0.0', // Should ideally come from PackageInfo
});
if (response.statusCode == 200) {
AppLogger.print('Email Login Success. Tokens received.');
final data = response.data['data'];
// Save secure data
await _storage.saveToken(data['access_token']);
// Note: auth/login might not return device_secret, handle if missing
if (data['device_secret'] != null) {
await _storage.saveDeviceSecret(data['device_secret']);
}
if (data['user']['email'] != null) {
await _storage.saveEmail(data['user']['email']);
}
AppSnackbar.showSuccess('مرحباً بك', 'تم تسجيل الدخول بنجاح');
// Navigate to Dashboard for reviewer, else Biometric Setup
if (email == 'reviewer@musadaq.jo') {
Get.offAllNamed(AppRoutes.MAIN);
} else {
Get.offAllNamed(AppRoutes.BIOMETRIC_SETUP);
}
}
} on DioException catch (e, stackTrace) {
AppLogger.error('Email Login Failed', e.response?.data, stackTrace);
String errorMessage = 'بيانات الدخول غير صحيحة';
if (e.response?.data != null && e.response?.data is Map) {
errorMessage = e.response?.data['message'] ?? errorMessage;
}
AppSnackbar.showError('خطأ', errorMessage);
} finally {
isLoading.value = false;
}
}
}