19
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:sefer_admin1/controller/functions/device_info.dart';
|
||||
import 'package:sefer_admin1/views/auth/login_page.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../constant/info.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../../views/admin/admin_home_page.dart';
|
||||
import '../../views/widgets/snackbar.dart';
|
||||
import '../functions/crud.dart';
|
||||
import '../functions/encrypt_decrypt.dart';
|
||||
|
||||
class OtpHelper extends GetxController {
|
||||
static final String _sendOtpUrl =
|
||||
@@ -73,27 +77,181 @@ class OtpHelper extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> checkAdminLogin() async {
|
||||
final deviceNumber =
|
||||
box.read(BoxName.fingerPrint); // خزّنه عند التشغيل أول مرة
|
||||
final phoneNumber = box.read(BoxName.adminPhone); // عند التحقق من OTP
|
||||
|
||||
if (deviceNumber != null && phoneNumber != null) {
|
||||
/// تسجيل الدخول بكلمة المرور والبصمة
|
||||
Future<bool> loginWithPassword(String password) async {
|
||||
try {
|
||||
final fingerprint = box.read(BoxName.fingerPrint);
|
||||
final response = await CRUD().post(
|
||||
link: _checkAdminLogin,
|
||||
payload: {
|
||||
"device_number": deviceNumber,
|
||||
"phone_number": phoneNumber,
|
||||
'fingerprint': fingerprint,
|
||||
'password': password,
|
||||
},
|
||||
);
|
||||
|
||||
if (response != "failure") {
|
||||
Get.offAll(() => AdminHomePage());
|
||||
if (response != 'failure') {
|
||||
// إذا كان الرد يتطلب OTP (السيرفر يرجعها بداخل message)
|
||||
final msg = response['message'];
|
||||
if (response['status'] == 'otp_required' || (msg is Map && msg['status'] == 'otp_required')) {
|
||||
String phone = (msg is Map ? msg['phone'] : response['phone']) ?? '';
|
||||
_showOtpDialog(phone, password, fingerprint);
|
||||
return false; // ننتظر إكمال الـ OTP
|
||||
}
|
||||
|
||||
// إذا نجح الدخول مباشرة
|
||||
return _handleLoginSuccess(response, password);
|
||||
} else {
|
||||
Get.offAll(() => AdminLoginPage());
|
||||
// سيقوم CRUD بإظهار الخطأ المناسب (مثل "حسابك قيد المراجعة")
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('LOGIN ERROR: $e');
|
||||
mySnackeBarError('حدث خطأ أثناء تسجيل الدخول: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// التحقق من OTP الخاص بتسجيل الدخول
|
||||
Future<void> verifyLoginOtp(
|
||||
String phone, String otp, String password, String fingerprint) async {
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: '${AppLink.server}/Admin/auth/verify_login.php',
|
||||
payload: {
|
||||
'phone': phone,
|
||||
'otp': otp,
|
||||
'fingerprint': fingerprint,
|
||||
},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
bool success = await _handleLoginSuccess(response, password);
|
||||
if (success) {
|
||||
Get.offAll(() => const AdminHomePage());
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('OTP VERIFY LOGIN ERROR: $e');
|
||||
mySnackeBarError('خطأ في التحقق من الرمز: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _handleLoginSuccess(dynamic response, String password) async {
|
||||
final msg = response['message'];
|
||||
final data = response['admin'] ?? (msg is Map ? msg['admin'] : null);
|
||||
final jwt = response['jwt'] ?? (msg is Map ? msg['jwt'] : null);
|
||||
|
||||
if (jwt != null) {
|
||||
await box.write(BoxName.jwt, c(jwt));
|
||||
}
|
||||
|
||||
if (data != null) {
|
||||
if (data['id'] != null) await box.write(BoxName.driverID, data['id']);
|
||||
if (data['role'] != null) {
|
||||
String role = data['role'].toString().trim();
|
||||
await box.write('admin_role', role);
|
||||
Log.print('Admin role saved: $role');
|
||||
}
|
||||
if (data['phone'] != null) await box.write(BoxName.adminPhone, data['phone']);
|
||||
}
|
||||
|
||||
await box.write(BoxName.phoneVerified, true);
|
||||
await box.write('admin_password', password);
|
||||
|
||||
mySnackbarSuccess('تم تسجيل الدخول بنجاح');
|
||||
return true;
|
||||
}
|
||||
|
||||
void _showOtpDialog(String phone, String password, String fingerprint) {
|
||||
String otpCode = '';
|
||||
Get.defaultDialog(
|
||||
title: 'رمز التحقق',
|
||||
content: Column(
|
||||
children: [
|
||||
Text('تم إرسال رمز التحقق إلى WhatsApp الخاص بك ($phone)'),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
onChanged: (val) => otpCode = val,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'أدخل الرمز هنا',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
textConfirm: 'تحقق',
|
||||
confirmTextColor: Colors.white,
|
||||
onConfirm: () {
|
||||
if (otpCode.length >= 5) {
|
||||
Get.back();
|
||||
verifyLoginOtp(phone, otpCode, password, fingerprint);
|
||||
} else {
|
||||
mySnackeBarError('الرجاء إدخال رمز صحيح');
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static bool _isAutoLoginAttempted = false;
|
||||
|
||||
Future<void> checkAdminLogin() async {
|
||||
final fingerprint = box.read(BoxName.fingerPrint);
|
||||
final password = box.read('admin_password');
|
||||
|
||||
if (fingerprint == null || password == null) {
|
||||
Get.offAll(() => const AdminLoginPage());
|
||||
return;
|
||||
}
|
||||
|
||||
// ─── أولاً: التحقق من وجود توكن JWT صالح ─────────
|
||||
// إذا وُجد توكن غير منتهي الصلاحية → ندخل مباشرة بدون استدعاء login.php
|
||||
final rawJwt = box.read(BoxName.jwt);
|
||||
if (rawJwt != null) {
|
||||
try {
|
||||
String token = r(rawJwt.toString()).split(AppInformation.addd)[0];
|
||||
if (!JwtDecoder.isExpired(token)) {
|
||||
Log.print('Valid JWT found, skipping login.php (no OTP needed)');
|
||||
Get.offAll(() => const AdminHomePage());
|
||||
return;
|
||||
}
|
||||
Log.print('JWT expired, need fresh login');
|
||||
} catch (e) {
|
||||
Log.print('JWT decode failed: \$e, need fresh login');
|
||||
}
|
||||
}
|
||||
|
||||
// ─── ثانياً: لا يوجد توكن صالح → استدعاء login.php ───
|
||||
final response = await CRUD().post(
|
||||
link: _checkAdminLogin,
|
||||
payload: {
|
||||
'fingerprint': fingerprint,
|
||||
'password': password,
|
||||
'is_renewal': '1',
|
||||
},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
final msg = response['message'];
|
||||
|
||||
if (response['status'] == 'otp_required' || (msg is Map && msg['status'] == 'otp_required')) {
|
||||
String phone = (msg is Map ? msg['phone'] : response['phone']) ?? '';
|
||||
_showOtpDialog(phone, password, fingerprint);
|
||||
return; // ننتظر إدخال رمز التحقق
|
||||
}
|
||||
|
||||
if (msg is Map && msg['jwt'] != null) {
|
||||
box.write(BoxName.jwt, c(msg['jwt']));
|
||||
if (msg['admin'] != null && msg['admin']['id'] != null) {
|
||||
box.write(BoxName.driverID, msg['admin']['id']);
|
||||
}
|
||||
} else if (response['jwt'] != null) {
|
||||
box.write(BoxName.jwt, c(response['jwt']));
|
||||
}
|
||||
Get.offAll(() => const AdminHomePage());
|
||||
} else {
|
||||
Get.offAll(() => AdminLoginPage());
|
||||
Log.print('Auto-login failed, redirecting to login page');
|
||||
Get.offAll(() => const AdminLoginPage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +262,15 @@ class OtpHelper extends GetxController {
|
||||
box.write(BoxName.fingerPrint, deviceFingerprint);
|
||||
});
|
||||
// تأجيل تنفيذ التنقل حتى تنتهي مرحلة البناء
|
||||
// ⚠️ محاولة واحدة فقط للتسجيل التلقائي لمنع الحلقة اللانهائية
|
||||
Future.microtask(() {
|
||||
if (_isAutoLoginAttempted) {
|
||||
// نحن في حلقة — نتوقف ونذهب لصفحة الدخول مباشرة
|
||||
Log.print('Auto-login already attempted, skipping to login page');
|
||||
return;
|
||||
}
|
||||
_isAutoLoginAttempted = true;
|
||||
|
||||
if (box.read(BoxName.phoneVerified) == true &&
|
||||
box.read(BoxName.adminPhone) != null) {
|
||||
checkAdminLogin();
|
||||
|
||||
Reference in New Issue
Block a user