Update: 2026-05-06 02:59:42
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
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';
|
||||
|
||||
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 {
|
||||
isLoading.value = true;
|
||||
phone.value = phoneNumber;
|
||||
|
||||
final response = await _dio.post('auth/mobile/request-otp', data: {
|
||||
'phone': phoneNumber,
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
Get.toNamed(AppRoutes.OTP_VERIFY);
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
Get.snackbar('خطأ', e.response?.data['message'] ?? 'فشل الاتصال بالخادم');
|
||||
} 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;
|
||||
}
|
||||
|
||||
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', // TODO: Get from package_info_plus
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data['data'];
|
||||
|
||||
// Save secure data
|
||||
await _storage.saveToken(data['access_token']);
|
||||
await _storage.saveDeviceSecret(data['device_secret']);
|
||||
|
||||
// Navigate to Dashboard or Biometric Setup
|
||||
Get.offAllNamed(AppRoutes.DASHBOARD);
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
Get.snackbar('خطأ', e.response?.data['message'] ?? 'رمز التحقق غير صحيح');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user