first commit
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:siro_driver/controller/auth/captin/login_captin_controller.dart';
|
||||
import 'package:siro_driver/controller/functions/crud.dart';
|
||||
import 'package:siro_driver/print.dart';
|
||||
import 'package:siro_driver/views/home/on_boarding_page.dart';
|
||||
import 'package:siro_driver/views/widgets/error_snakbar.dart';
|
||||
|
||||
import '../../../constant/box_name.dart';
|
||||
import '../../../constant/links.dart';
|
||||
import '../../../main.dart';
|
||||
import '../../../views/auth/syria/registration_view.dart';
|
||||
|
||||
// --- Helper Class for Phone Authentication ---
|
||||
|
||||
class PhoneAuthHelper {
|
||||
// Define your server URLs
|
||||
static final String _baseUrl = '${AppLink.server}/auth/syria/driver/';
|
||||
static final String _sendOtpUrl = '${_baseUrl}sendWhatsAppDriver.php';
|
||||
static final String _verifyOtpUrl = '${_baseUrl}verifyOtp.php';
|
||||
static final String _registerUrl = '${_baseUrl}register_driver.php';
|
||||
static String formatSyrianPhone(String phone) {
|
||||
// Remove spaces, symbols, +, -, ()
|
||||
phone = phone.replaceAll(RegExp(r'[ \-\(\)\+]'), '').trim();
|
||||
|
||||
// Normalize 00963 → 963
|
||||
if (phone.startsWith('00963')) {
|
||||
phone = phone.replaceFirst('00963', '963');
|
||||
}
|
||||
|
||||
// Normalize 0963 → 963
|
||||
if (phone.startsWith('0963')) {
|
||||
phone = phone.replaceFirst('0963', '963');
|
||||
}
|
||||
if (phone.startsWith('096309')) {
|
||||
phone = phone.replaceFirst('096309', '963');
|
||||
}
|
||||
|
||||
// NEW: Fix 96309xxxx → 9639xxxx
|
||||
if (phone.startsWith('96309')) {
|
||||
phone = '9639' + phone.substring(5); // remove the "0" after 963
|
||||
}
|
||||
|
||||
// If starts with 9630 → correct to 9639
|
||||
if (phone.startsWith('9630')) {
|
||||
phone = '9639' + phone.substring(4);
|
||||
}
|
||||
|
||||
// If already in correct format: 9639xxxxxxxx
|
||||
if (phone.startsWith('9639') && phone.length == 12) {
|
||||
return phone;
|
||||
}
|
||||
|
||||
// If starts with 963 but missing the 9
|
||||
if (phone.startsWith('963') && phone.length > 3) {
|
||||
// Ensure it begins with 9639
|
||||
if (!phone.startsWith('9639')) {
|
||||
phone = '9639' + phone.substring(3);
|
||||
}
|
||||
return phone;
|
||||
}
|
||||
|
||||
// If starts with 09xxxxxxxx → 9639xxxxxxxx
|
||||
if (phone.startsWith('09')) {
|
||||
return '963' + phone.substring(1);
|
||||
}
|
||||
|
||||
// If 9xxxxxxxx (9 digits)
|
||||
if (phone.startsWith('9') && phone.length == 9) {
|
||||
return '963' + phone;
|
||||
}
|
||||
|
||||
// If starts with incorrect 0xxxxxxx → assume Syrian and fix
|
||||
if (phone.startsWith('0') && phone.length == 10) {
|
||||
return '963' + phone.substring(1);
|
||||
}
|
||||
|
||||
return phone;
|
||||
}
|
||||
|
||||
/// Sends an OTP to the provided phone number.
|
||||
static Future<bool> sendOtp(String phoneNumber) async {
|
||||
try {
|
||||
final fixedPhone = formatSyrianPhone(phoneNumber);
|
||||
Log.print('fixedPhone: $fixedPhone');
|
||||
|
||||
final response = await CRUD().post(
|
||||
link: _sendOtpUrl,
|
||||
payload: {'receiver': fixedPhone},
|
||||
);
|
||||
Log.print('fixedPhone: ${fixedPhone}');
|
||||
|
||||
if (response != 'failure') {
|
||||
final data = (response);
|
||||
Log.print('data: ${data}');
|
||||
// if (data['status'] == 'success') {
|
||||
mySnackbarSuccess('An OTP has been sent to your number.'.tr);
|
||||
return true;
|
||||
// } else {
|
||||
// mySnackeBarError(data['message'] ?? 'Failed to send OTP.');
|
||||
// return false;
|
||||
// }
|
||||
} else {
|
||||
mySnackeBarError('Server error. Please try again.'.tr);
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
// mySnackeBarError('An error occurred: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Verifies the OTP and logs the user in.
|
||||
static Future<void> verifyOtp(String phoneNumber, String otpCode) async {
|
||||
try {
|
||||
final fixedPhone = formatSyrianPhone(phoneNumber);
|
||||
Log.print('fixedPhone: $fixedPhone');
|
||||
final response = await CRUD().post(
|
||||
link: _verifyOtpUrl,
|
||||
payload: {
|
||||
'phone_number': fixedPhone,
|
||||
'otp': otpCode,
|
||||
},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
final data = response;
|
||||
|
||||
if (data['status'] == 'success') {
|
||||
final isRegistered = data['message']['isRegistered'] ?? false;
|
||||
box.write(BoxName.phoneVerified, '1');
|
||||
box.write(BoxName.phoneDriver, phoneNumber);
|
||||
box.write(BoxName.driverID, data['message']['driverID']);
|
||||
|
||||
if (isRegistered) {
|
||||
// ✅ السائق مسجل مسبقًا - سجل دخوله واذهب إلى الصفحة الرئيسية
|
||||
final driver = data['message']['driver'];
|
||||
// mySnackbarSuccess('Welcome back, ${driver['first_name']}!');
|
||||
|
||||
// حفظ بيانات السائق إذا أردت:
|
||||
box.write(BoxName.driverID, driver['id']);
|
||||
box.write(BoxName.emailDriver, driver['email']);
|
||||
|
||||
await Get.find<LoginDriverController>().loginWithGoogleCredential(
|
||||
driver['id'].toString(), driver['email'].toString());
|
||||
} else {
|
||||
// ✅ رقم الهاتف تم التحقق منه لكن السائق غير مسجل
|
||||
// mySnackbarSuccess('Phone verified. Please complete registration.');
|
||||
// Get.offAll(() => SyrianCardAI());
|
||||
Get.to(() => RegistrationView());
|
||||
}
|
||||
} else {
|
||||
mySnackeBarError(data['message'] ?? 'Verification failed.');
|
||||
}
|
||||
} else {
|
||||
mySnackeBarError('Server error. Please try again.');
|
||||
}
|
||||
} catch (e) {
|
||||
mySnackeBarError('An error occurred: $e');
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> registerUser({
|
||||
required String phoneNumber,
|
||||
required String firstName,
|
||||
required String lastName,
|
||||
String? email,
|
||||
}) async {
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: _registerUrl,
|
||||
payload: {
|
||||
'phone_number': phoneNumber,
|
||||
'first_name': firstName,
|
||||
'last_name': lastName,
|
||||
'email': email ?? '', // Send empty string if null
|
||||
},
|
||||
);
|
||||
final data = (response);
|
||||
if (data != 'failure') {
|
||||
// Registration successful, log user in
|
||||
await _handleSuccessfulLogin(data['message']);
|
||||
} else {
|
||||
mySnackeBarError(
|
||||
"User with this phone number or email already exists.".tr);
|
||||
}
|
||||
} catch (e) {
|
||||
mySnackeBarError('An error occurred: $e');
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> _handleSuccessfulLogin(
|
||||
Map<String, dynamic> userData) async {
|
||||
mySnackbarSuccess('Welcome, ${userData['first_name']}!');
|
||||
|
||||
// Save user data to local storage (Hive box) using new keys
|
||||
box.write(BoxName.passengerID, userData['id']);
|
||||
box.write(BoxName.nameDriver, userData['first_name']);
|
||||
box.write(BoxName.lastNameDriver, userData['last_name']);
|
||||
box.write(BoxName.emailDriver, userData['email']);
|
||||
box.write(BoxName.phoneDriver, userData['phone']);
|
||||
|
||||
Get.offAll(() => OnBoardingPage()); // Navigate to home
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user