first commit
This commit is contained in:
56
lib/controller/auth/login_controller.dart
Normal file
56
lib/controller/auth/login_controller.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import 'otp_helper.dart';
|
||||
|
||||
class OtpVerificationAdmin extends StatefulWidget {
|
||||
final String phone;
|
||||
const OtpVerificationAdmin({required this.phone});
|
||||
|
||||
@override
|
||||
State<OtpVerificationAdmin> createState() => _OtpVerificationAdminState();
|
||||
}
|
||||
|
||||
class _OtpVerificationAdminState extends State<OtpVerificationAdmin> {
|
||||
final _otpController = TextEditingController();
|
||||
bool _isLoading = false;
|
||||
|
||||
Future<void> _verifyOtp() async {
|
||||
setState(() => _isLoading = true);
|
||||
|
||||
final otpHelper = OtpHelper();
|
||||
await otpHelper.verifyOtp(widget.phone, _otpController.text.trim());
|
||||
// if (success) {
|
||||
// Get.offAllNamed('/admin-dashboard');
|
||||
// } else {
|
||||
// Get.snackbar('خطأ', 'رمز التحقق غير صحيح');
|
||||
// }
|
||||
setState(() => _isLoading = false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('التحقق من الرمز')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _otpController,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: const InputDecoration(labelText: 'رمز التحقق'),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
_isLoading
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: _verifyOtp,
|
||||
child: const Text('تحقق وأدخل'),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
114
lib/controller/auth/otp_helper.dart
Normal file
114
lib/controller/auth/otp_helper.dart
Normal file
@@ -0,0 +1,114 @@
|
||||
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 '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../../views/admin/admin_home_page.dart';
|
||||
import '../../views/widgets/snackbar.dart';
|
||||
import '../functions/crud.dart';
|
||||
|
||||
class OtpHelper extends GetxController {
|
||||
static final String _sendOtpUrl =
|
||||
'${AppLink.server}/Admin/auth/send_otp_admin.php';
|
||||
static final String _verifyOtpUrl =
|
||||
'${AppLink.server}/Admin/auth/verify_otp_admin.php';
|
||||
static final String _checkAdminLogin =
|
||||
'${AppLink.server}/Admin/auth/login.php';
|
||||
|
||||
/// إرسال OTP
|
||||
static Future<bool> sendOtp(String phoneNumber) async {
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: _sendOtpUrl,
|
||||
payload: {'receiver': phoneNumber},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
mySnackeBarError('تم إرسال رمز التحقق إلى رقمك عبر WhatsApp');
|
||||
return true;
|
||||
} else {
|
||||
mySnackeBarError('حدث خطأ من الخادم. حاول مجددًا.');
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('OTP SEND ERROR: $e');
|
||||
mySnackeBarError('حدث خطأ أثناء الإرسال: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// التحقق من OTP
|
||||
Future<void> verifyOtp(String phoneNumber, String otp) async {
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: _verifyOtpUrl,
|
||||
payload: {
|
||||
'phone_number': phoneNumber,
|
||||
'otp': otp,
|
||||
'device_number': box.read(BoxName.fingerPrint)
|
||||
},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
if (response['status'] == 'success') {
|
||||
box.write(BoxName.phoneVerified, true);
|
||||
box.write(BoxName.adminPhone, phoneNumber);
|
||||
|
||||
mySnackbarSuccess('تم التحقق من الرقم بنجاح');
|
||||
await checkAdminLogin();
|
||||
} else {
|
||||
mySnackeBarError(response['message'] ?? 'فشل في التحقق.');
|
||||
}
|
||||
} else {
|
||||
mySnackeBarError('فشل من الخادم. حاول مرة أخرى.');
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('OTP VERIFY ERROR: $e');
|
||||
mySnackeBarError('خطأ في التحقق: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> checkAdminLogin() async {
|
||||
final deviceNumber =
|
||||
box.read(BoxName.fingerPrint); // خزّنه عند التشغيل أول مرة
|
||||
final phoneNumber = box.read(BoxName.adminPhone); // عند التحقق من OTP
|
||||
|
||||
if (deviceNumber != null && phoneNumber != null) {
|
||||
final response = await CRUD().post(
|
||||
link: _checkAdminLogin,
|
||||
payload: {
|
||||
"device_number": deviceNumber,
|
||||
"phone_number": phoneNumber,
|
||||
},
|
||||
);
|
||||
|
||||
if (response != "failure") {
|
||||
Get.offAll(() => AdminHomePage());
|
||||
} else {
|
||||
Get.offAll(() => AdminLoginPage());
|
||||
}
|
||||
} else {
|
||||
Get.offAll(() => AdminLoginPage());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
DeviceHelper.getDeviceFingerprint().then((deviceFingerprint) {
|
||||
box.write(BoxName.fingerPrint, deviceFingerprint);
|
||||
});
|
||||
// تأجيل تنفيذ التنقل حتى تنتهي مرحلة البناء
|
||||
Future.microtask(() {
|
||||
if (box.read(BoxName.phoneVerified) == true &&
|
||||
box.read(BoxName.adminPhone) != null) {
|
||||
checkAdminLogin();
|
||||
} else {
|
||||
Get.offAll(() => AdminLoginPage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user