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)
|
||||
),
|
||||
)),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user