Update: 2026-05-13 22:58:30
This commit is contained in:
@@ -6,6 +6,7 @@ class SecureStorage {
|
||||
static const String _keyToken = 'jwt_token';
|
||||
static const String _keyDeviceSecret = 'device_secret';
|
||||
static const String _keyUserId = 'user_id';
|
||||
static const String _keyEmail = 'user_email';
|
||||
|
||||
Future<void> saveToken(String token) async {
|
||||
await _storage.write(key: _keyToken, value: token);
|
||||
@@ -23,6 +24,14 @@ class SecureStorage {
|
||||
return await _storage.read(key: _keyDeviceSecret);
|
||||
}
|
||||
|
||||
Future<void> saveEmail(String email) async {
|
||||
await _storage.write(key: _keyEmail, value: email);
|
||||
}
|
||||
|
||||
Future<String?> getEmail() async {
|
||||
return await _storage.read(key: _keyEmail);
|
||||
}
|
||||
|
||||
Future<void> clearAll() async {
|
||||
await _storage.deleteAll();
|
||||
}
|
||||
|
||||
@@ -96,11 +96,18 @@ class AuthController extends GetxController {
|
||||
// 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
|
||||
Get.offAllNamed(AppRoutes.BIOMETRIC_SETUP);
|
||||
// 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);
|
||||
@@ -109,4 +116,72 @@ class AuthController extends GetxController {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,12 +31,20 @@ class BiometricController extends GetxController {
|
||||
}
|
||||
|
||||
Future<void> authenticateAndGoToDashboard() async {
|
||||
// REVIEWER BYPASS: If the user is the app reviewer, skip biometrics entirely
|
||||
final userEmail = await _storage.getEmail();
|
||||
if (userEmail == 'reviewer@musadaq.jo') {
|
||||
AppLogger.print('Reviewer account detected. Bypassing biometrics.');
|
||||
Get.offAllNamed(AppRoutes.MAIN);
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure we have checked biometric status first
|
||||
await checkBiometrics();
|
||||
|
||||
if (!isBiometricAvailable.value) {
|
||||
AppLogger.print('Biometrics not available, going directly to dashboard.');
|
||||
Get.offAllNamed(AppRoutes.DASHBOARD);
|
||||
Get.offAllNamed(AppRoutes.MAIN);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ class PhoneInputView extends StatelessWidget {
|
||||
|
||||
final AuthController controller = Get.put(AuthController());
|
||||
final TextEditingController phoneController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -36,24 +37,47 @@ 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,
|
||||
keyboardType: TextInputType.phone,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
textDirection: TextDirection.ltr,
|
||||
onChanged: (val) => controller.phone.value = val,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'رقم الهاتف',
|
||||
prefixIcon: const Icon(Icons.phone),
|
||||
labelText: 'رقم الهاتف أو البريد الإلكتروني',
|
||||
prefixIcon: const Icon(Icons.person_outline),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
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),
|
||||
],
|
||||
);
|
||||
}),
|
||||
Obx(() => ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
@@ -63,10 +87,24 @@ class PhoneInputView extends StatelessWidget {
|
||||
),
|
||||
onPressed: controller.isLoading.value
|
||||
? null
|
||||
: () => controller.requestOtp(phoneController.text),
|
||||
: () {
|
||||
if (controller.phone.value.contains('@')) {
|
||||
controller.loginWithEmail(
|
||||
controller.phone.value,
|
||||
passwordController.text
|
||||
);
|
||||
} else {
|
||||
controller.requestOtp(phoneController.text);
|
||||
}
|
||||
},
|
||||
child: controller.isLoading.value
|
||||
? const CircularProgressIndicator(color: Colors.white)
|
||||
: const Text('إرسال رمز التحقق', style: TextStyle(fontSize: 16)),
|
||||
: Text(
|
||||
controller.phone.value.contains('@')
|
||||
? 'تسجيل الدخول'
|
||||
: 'إرسال رمز التحقق',
|
||||
style: const TextStyle(fontSize: 16)
|
||||
),
|
||||
)),
|
||||
],
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user