Update: 2026-05-25 21:44:11
This commit is contained in:
@@ -12,7 +12,7 @@ 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;
|
||||
|
||||
@@ -23,20 +23,20 @@ class AuthController extends GetxController {
|
||||
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);
|
||||
normalizedPhone = '962${normalizedPhone.substring(1)}';
|
||||
} else if (normalizedPhone.startsWith('7')) {
|
||||
normalizedPhone = '962' + normalizedPhone;
|
||||
normalizedPhone = '962$normalizedPhone';
|
||||
}
|
||||
|
||||
|
||||
phone.value = normalizedPhone;
|
||||
|
||||
|
||||
final response = await _dio.post('auth/mobile/request-otp', data: {
|
||||
'phone': normalizedPhone,
|
||||
});
|
||||
@@ -60,12 +60,12 @@ class AuthController extends GetxController {
|
||||
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;
|
||||
@@ -92,16 +92,16 @@ class AuthController extends GetxController {
|
||||
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);
|
||||
@@ -111,7 +111,8 @@ class AuthController extends GetxController {
|
||||
}
|
||||
} on DioException catch (e, stackTrace) {
|
||||
AppLogger.error('OTP Verify Failed', e.response?.data, stackTrace);
|
||||
AppSnackbar.showError('خطأ', e.response?.data['message'] ?? 'رمز التحقق غير صحيح');
|
||||
AppSnackbar.showError(
|
||||
'خطأ', e.response?.data['message'] ?? 'رمز التحقق غير صحيح');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
@@ -120,7 +121,8 @@ class AuthController extends GetxController {
|
||||
Future<void> loginWithEmail(String email, String password) async {
|
||||
try {
|
||||
if (email.trim().isEmpty || password.trim().isEmpty) {
|
||||
AppSnackbar.showError('خطأ', 'الرجاء إدخال البريد الإلكتروني وكلمة المرور');
|
||||
AppSnackbar.showError(
|
||||
'خطأ', 'الرجاء إدخال البريد الإلكتروني وكلمة المرور');
|
||||
return;
|
||||
}
|
||||
isLoading.value = true;
|
||||
@@ -129,7 +131,7 @@ class AuthController extends GetxController {
|
||||
final deviceInfo = DeviceInfoPlugin();
|
||||
String deviceId = '';
|
||||
String deviceName = '';
|
||||
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
final androidInfo = await deviceInfo.androidInfo;
|
||||
deviceId = androidInfo.id;
|
||||
@@ -150,22 +152,31 @@ class AuthController extends GetxController {
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
AppLogger.print('Email Login Success. Tokens received.');
|
||||
final data = response.data['data'];
|
||||
|
||||
|
||||
if (data['otp_required'] == true) {
|
||||
AppLogger.print('Email Login verification required via OTP.');
|
||||
phone.value = data['phone'] ?? '';
|
||||
AppSnackbar.showSuccess('نجاح', 'تم إرسال رمز التحقق إلى رقم هاتفك المسجل');
|
||||
Get.toNamed(AppRoutes.OTP_VERIFY);
|
||||
return;
|
||||
}
|
||||
|
||||
AppLogger.print('Email Login Success. Tokens received.');
|
||||
|
||||
// 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);
|
||||
|
||||
@@ -6,7 +6,7 @@ class PhoneInputView extends StatelessWidget {
|
||||
PhoneInputView({super.key});
|
||||
|
||||
final AuthController controller = Get.put(AuthController());
|
||||
final TextEditingController phoneController = TextEditingController();
|
||||
final TextEditingController emailController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
|
||||
@override
|
||||
@@ -37,47 +37,38 @@ class PhoneInputView extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'أدخل رقم هاتفك أو البريد الإلكتروني لتسجيل الدخول',
|
||||
'أدخل البريد الإلكتروني وكلمة المرور لتسجيل الدخول',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
TextField(
|
||||
controller: phoneController,
|
||||
controller: emailController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
textDirection: TextDirection.ltr,
|
||||
onChanged: (val) => controller.phone.value = val,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'رقم الهاتف أو البريد الإلكتروني',
|
||||
prefixIcon: const Icon(Icons.person_outline),
|
||||
labelText: 'البريد الإلكتروني',
|
||||
prefixIcon: const Icon(Icons.email_outlined),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Obx(() {
|
||||
final isEmail = controller.phone.value.contains('@');
|
||||
if (!isEmail) return const SizedBox.shrink();
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: passwordController,
|
||||
obscureText: true,
|
||||
textDirection: TextDirection.ltr,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'كلمة المرور',
|
||||
prefixIcon: const Icon(Icons.lock_outline),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
],
|
||||
);
|
||||
}),
|
||||
TextField(
|
||||
controller: passwordController,
|
||||
obscureText: true,
|
||||
textDirection: TextDirection.ltr,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'كلمة المرور',
|
||||
prefixIcon: const Icon(Icons.lock_outline),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Obx(() => ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
@@ -88,22 +79,16 @@ class PhoneInputView extends StatelessWidget {
|
||||
onPressed: controller.isLoading.value
|
||||
? null
|
||||
: () {
|
||||
if (controller.phone.value.contains('@')) {
|
||||
controller.loginWithEmail(
|
||||
controller.phone.value,
|
||||
passwordController.text
|
||||
);
|
||||
} else {
|
||||
controller.requestOtp(phoneController.text);
|
||||
}
|
||||
controller.loginWithEmail(
|
||||
emailController.text,
|
||||
passwordController.text
|
||||
);
|
||||
},
|
||||
child: controller.isLoading.value
|
||||
? const CircularProgressIndicator(color: Colors.white)
|
||||
: Text(
|
||||
controller.phone.value.contains('@')
|
||||
? 'تسجيل الدخول'
|
||||
: 'إرسال رمز التحقق',
|
||||
style: const TextStyle(fontSize: 16)
|
||||
: const Text(
|
||||
'تسجيل الدخول',
|
||||
style: TextStyle(fontSize: 16)
|
||||
),
|
||||
)),
|
||||
],
|
||||
|
||||
@@ -9,8 +9,8 @@ import 'tenants_management_controller.dart';
|
||||
class AddTenantController extends GetxController {
|
||||
final nameController = TextEditingController();
|
||||
final emailController = TextEditingController();
|
||||
final phoneController = TextEditingController();
|
||||
final managerNameController = TextEditingController();
|
||||
final managerEmailController = TextEditingController();
|
||||
final managerPasswordController = TextEditingController();
|
||||
|
||||
var isSubmitting = false.obs;
|
||||
@@ -20,8 +20,8 @@ class AddTenantController extends GetxController {
|
||||
void onClose() {
|
||||
nameController.dispose();
|
||||
emailController.dispose();
|
||||
phoneController.dispose();
|
||||
managerNameController.dispose();
|
||||
managerEmailController.dispose();
|
||||
managerPasswordController.dispose();
|
||||
super.onClose();
|
||||
}
|
||||
@@ -29,11 +29,11 @@ class AddTenantController extends GetxController {
|
||||
Future<void> submit() async {
|
||||
final name = nameController.text.trim();
|
||||
final email = emailController.text.trim();
|
||||
final phone = phoneController.text.trim();
|
||||
final managerName = managerNameController.text.trim();
|
||||
final managerEmail = managerEmailController.text.trim();
|
||||
final managerPassword = managerPasswordController.text;
|
||||
|
||||
if (name.isEmpty || email.isEmpty || managerName.isEmpty || managerEmail.isEmpty || managerPassword.isEmpty) {
|
||||
if (name.isEmpty || email.isEmpty || phone.isEmpty || managerName.isEmpty || managerPassword.isEmpty) {
|
||||
AppSnackbar.showWarning('تنبيه', 'الرجاء إدخال جميع البيانات المطلوبة');
|
||||
return;
|
||||
}
|
||||
@@ -43,8 +43,8 @@ class AddTenantController extends GetxController {
|
||||
final response = await _dio.post('tenants/create', data: {
|
||||
'name': name,
|
||||
'email': email,
|
||||
'phone': phone,
|
||||
'manager_name': managerName,
|
||||
'manager_email': managerEmail,
|
||||
'manager_password': managerPassword,
|
||||
});
|
||||
|
||||
|
||||
@@ -36,29 +36,29 @@ class AddTenantView extends StatelessWidget {
|
||||
const SizedBox(height: 16),
|
||||
_buildTextField(
|
||||
controller: controller.emailController,
|
||||
label: 'البريد الإلكتروني للمكتب',
|
||||
label: 'البريد الإلكتروني للعمل',
|
||||
icon: Icons.email,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
isDark: isDark,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text(
|
||||
'بيانات مدير المكتب',
|
||||
'بيانات مدير المكتب المسؤول',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildTextField(
|
||||
controller: controller.managerNameController,
|
||||
label: 'اسم المدير',
|
||||
label: 'اسم المدير الكامل',
|
||||
icon: Icons.person,
|
||||
isDark: isDark,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildTextField(
|
||||
controller: controller.managerEmailController,
|
||||
label: 'البريد الإلكتروني للمدير',
|
||||
icon: Icons.alternate_email,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
controller: controller.phoneController,
|
||||
label: 'رقم هاتف المدير (لتسجيل الدخول OTP)',
|
||||
icon: Icons.phone,
|
||||
keyboardType: TextInputType.phone,
|
||||
isDark: isDark,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
Reference in New Issue
Block a user