25-10-2/1
This commit is contained in:
@@ -2,6 +2,7 @@ import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:Intaleq/constant/api_key.dart';
|
||||
import 'package:Intaleq/views/auth/otp_page.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'package:Intaleq/constant/info.dart';
|
||||
@@ -173,149 +174,161 @@ class LoginController extends GetxController {
|
||||
|
||||
await AppInitializer().getKey();
|
||||
|
||||
final refreshToken = decodedResponse1['refresh_token'];
|
||||
await storage.write(key: BoxName.refreshToken, value: refreshToken);
|
||||
} else {}
|
||||
// final refreshToken = decodedResponse1['refresh_token'];
|
||||
// await storage.write(key: BoxName.refreshToken, value: refreshToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loginUsingCredentials(String passengerID, email) async {
|
||||
Future<void> loginUsingCredentials(String passengerID, String email) async {
|
||||
isloading = true;
|
||||
update();
|
||||
// bool isTokenExpired = JwtDecoder.isExpired(
|
||||
// r(box.read(BoxName.jwt)).toString().split(AppInformation.addd)[0]);
|
||||
await getJWT();
|
||||
|
||||
// if (isTokenExpired) {
|
||||
// // Log.print('isTokenExpired loginUsingCredentials: ${isTokenExpired}');
|
||||
// await getJWT();
|
||||
// }
|
||||
try {
|
||||
// 1) استعلام تسجيل الدخول
|
||||
final res = await CRUD().get(
|
||||
link: AppLink.loginFromGooglePassenger,
|
||||
payload: {
|
||||
'email': email,
|
||||
'id': passengerID, // استخدم المعامل مباشرة
|
||||
'platform': Platform.isAndroid ? 'android' : 'ios',
|
||||
'appName': AppInformation.appName,
|
||||
},
|
||||
);
|
||||
|
||||
var res =
|
||||
await CRUD().get(link: AppLink.loginFromGooglePassenger, payload: {
|
||||
'email': email.toString(),
|
||||
'id': passengerID.toString(),
|
||||
"platform": Platform.isAndroid ? 'android' : 'ios',
|
||||
"appName": AppInformation.appName,
|
||||
});
|
||||
if (res == 'Failure') {
|
||||
// Get.offAll(SmsSignupEgypt());
|
||||
if (res == 'Failure') {
|
||||
Get.snackbar("User does not exist.".tr, '',
|
||||
backgroundColor: Colors.red);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2) فك JSON مرة واحدة
|
||||
final decoded = jsonDecode(res);
|
||||
if (decoded is! Map || decoded.isEmpty) return;
|
||||
|
||||
final status = (decoded['status'] ?? '').toString();
|
||||
final data = (decoded['data'] as List?)?.firstOrNull as Map? ?? {};
|
||||
if (status != 'success' || data['verified'].toString() != '1') {
|
||||
// غير مُفعل -> أذهب للتسجيل بالـ SMS
|
||||
Get.offAll(() => PhoneNumberScreen());
|
||||
return;
|
||||
}
|
||||
|
||||
// 3) كتابة القيم (خفيفة)
|
||||
box.write(BoxName.isVerified, '1');
|
||||
box.write(BoxName.email, data['email']);
|
||||
box.write(BoxName.phone, data['phone']);
|
||||
box.write(BoxName.name, data['first_name']);
|
||||
box.write(BoxName.isTest, '1');
|
||||
box.write(BoxName.package, data['package']);
|
||||
box.write(BoxName.promo, data['promo']);
|
||||
box.write(BoxName.discount, data['discount']);
|
||||
box.write(BoxName.validity, data['validity']);
|
||||
box.write(BoxName.isInstall, data['isInstall'] ?? 'none');
|
||||
box.write(BoxName.isGiftToken, data['isGiftToken'] ?? 'none');
|
||||
if (data['inviteCode'] != null) {
|
||||
box.write(BoxName.inviteCode, data['inviteCode'].toString());
|
||||
}
|
||||
// مهم: تأكد من passengerID في الـ box
|
||||
box.write(BoxName.passengerID, passengerID);
|
||||
|
||||
// 4) نفّذ عمليات مكلفة بالتوازي: getTokens + fingerprint
|
||||
final results = await Future.wait([
|
||||
CRUD().get(link: AppLink.getTokens, payload: {
|
||||
'passengerID': passengerID, // FIX: لا تستخدم box هنا
|
||||
}),
|
||||
DeviceHelper.getDeviceFingerprint(),
|
||||
]);
|
||||
await box.write(BoxName.firstTimeLoadKey, 'false');
|
||||
final tokenResp = results[0];
|
||||
final fingerPrint = (results[1] ?? '').toString();
|
||||
await storage.write(key: BoxName.fingerPrint, value: fingerPrint);
|
||||
|
||||
if (email != '962798583052@intaleqapp.com' && tokenResp != 'failure') {
|
||||
final tokenJson = jsonDecode(tokenResp);
|
||||
final serverToken = tokenJson['message']?['token']?.toString() ?? '';
|
||||
// Log.print('serverToken: ${serverToken}');
|
||||
final localFcm = (box.read(BoxName.tokenFCM) ?? '').toString();
|
||||
// Log.print('localFcm: ${localFcm}');
|
||||
|
||||
// 5) اختلاف الجهاز -> تحقّق OTP
|
||||
if (serverToken.isNotEmpty && serverToken != localFcm) {
|
||||
final goVerify = await _confirmDeviceChangeDialog();
|
||||
if (goVerify == true) {
|
||||
await Get.to(() => OtpVerificationPage(
|
||||
phone: data['phone'].toString(),
|
||||
deviceToken: fingerPrint,
|
||||
token: tokenResp.toString(),
|
||||
ptoken: serverToken,
|
||||
));
|
||||
// بعد العودة من OTP (نجح/فشل)، أخرج من الميثود كي لا يحصل offAll مرتين
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 6) منطق الدعوة (إن وُجد) ثم الانتقال
|
||||
final invite = box.read(BoxName.inviteCode)?.toString() ?? 'none';
|
||||
final isInstall = box.read(BoxName.isInstall)?.toString() ?? '0';
|
||||
|
||||
if (invite != 'none' && isInstall != '1') {
|
||||
try {
|
||||
await CRUD().post(link: AppLink.updatePassengersInvitation, payload: {
|
||||
"inviteCode": invite,
|
||||
"passengerID": passengerID,
|
||||
});
|
||||
await Get.defaultDialog(
|
||||
title: 'Invitation Used'.tr,
|
||||
middleText: "Your invite code was successfully applied!".tr,
|
||||
textConfirm: "OK".tr,
|
||||
confirmTextColor: Colors.white,
|
||||
onConfirm: () async {
|
||||
try {
|
||||
await CRUD().post(link: AppLink.addPassengersPromo, payload: {
|
||||
"promoCode":
|
||||
'I-${(box.read(BoxName.name)).toString().split(' ').first}',
|
||||
"amount": '25',
|
||||
"passengerID": passengerID,
|
||||
"description": 'promo first'
|
||||
});
|
||||
} catch (e) {
|
||||
addError(
|
||||
e.toString(), 'promo on invitation in login_controller');
|
||||
} finally {
|
||||
Get.offAll(() => const MapPagePassenger());
|
||||
}
|
||||
},
|
||||
);
|
||||
return;
|
||||
} catch (_) {
|
||||
// حتى لو فشل، كمل للصفحة الرئيسية
|
||||
}
|
||||
}
|
||||
|
||||
Get.offAll(() => const MapPagePassenger());
|
||||
} catch (e, st) {
|
||||
addError('$e', 'loginUsingCredentials');
|
||||
Get.snackbar('Error', e.toString(), backgroundColor: Colors.redAccent);
|
||||
} finally {
|
||||
isloading = false;
|
||||
update();
|
||||
Get.snackbar("User does not exist.".tr, '', backgroundColor: Colors.red);
|
||||
} else {
|
||||
// Log.print('res: ${res}');
|
||||
var jsonDecoeded = jsonDecode(res);
|
||||
|
||||
if (jsonDecoeded.isNotEmpty) {
|
||||
var d = jsonDecoeded['data'][0];
|
||||
if (jsonDecoeded['status'] == 'success' &&
|
||||
d['verified'].toString() == '1') {
|
||||
//
|
||||
|
||||
box.write(BoxName.isVerified, '1');
|
||||
box.write(BoxName.email, d['email']);
|
||||
box.write(BoxName.phone, d['phone']);
|
||||
box.write(BoxName.name, d['first_name']);
|
||||
box.write(BoxName.isTest, '1');
|
||||
box.write(BoxName.package, d['package']);
|
||||
box.write(BoxName.promo, d['promo']);
|
||||
box.write(BoxName.discount, d['discount']);
|
||||
box.write(BoxName.validity, d['validity']);
|
||||
box.write(BoxName.isInstall, d['isInstall'] ?? 'none');
|
||||
box.write(BoxName.isGiftToken, d['isGiftToken'] ?? 'none');
|
||||
box.write(BoxName.firstTimeLoadKey, 'false');
|
||||
d['inviteCode'] != null
|
||||
? box.write(
|
||||
BoxName.inviteCode, (d['inviteCode'].toString()) ?? 'none')
|
||||
: null;
|
||||
|
||||
var token = await CRUD().get(link: AppLink.getTokens, payload: {
|
||||
'passengerID': box.read(BoxName.passengerID).toString()
|
||||
});
|
||||
var fingerPrint = await DeviceHelper.getDeviceFingerprint();
|
||||
Log.print('fingerPrint 0: ${fingerPrint}');
|
||||
await storage.write(key: BoxName.fingerPrint, value: fingerPrint);
|
||||
|
||||
if (email == '962798583052@intaleqapp.com') {
|
||||
} else {
|
||||
if (token != 'failure') {
|
||||
if ((jsonDecode(token)['message']['token'].toString()) !=
|
||||
box.read(BoxName.tokenFCM)) {
|
||||
await Get.defaultDialog(
|
||||
barrierDismissible: false,
|
||||
title: 'Device Change Detected'.tr,
|
||||
middleText: 'Please verify your identity'.tr,
|
||||
textConfirm: 'Verify'.tr,
|
||||
confirmTextColor: Colors.white,
|
||||
onConfirm: () {
|
||||
// Get.back();
|
||||
// انتقل لصفحة OTP الجديدة
|
||||
Get.to(
|
||||
() => OtpVerificationPage(
|
||||
phone: d['phone'].toString(),
|
||||
deviceToken: fingerPrint.toString(),
|
||||
token: token.toString(),
|
||||
ptoken:
|
||||
jsonDecode(token)['message']['token'].toString(),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
} // Logging to check if inviteCode is written correctly
|
||||
|
||||
if (d['inviteCode'] != 'none' &&
|
||||
d['inviteCode'] != null &&
|
||||
// box.read(BoxName.inviteCode).toString() != 'none' &&
|
||||
box.read(BoxName.isInstall).toString() != '1') {
|
||||
await CRUD()
|
||||
.post(link: AppLink.updatePassengersInvitation, payload: {
|
||||
"inviteCode": (box.read(BoxName.inviteCode).toString()),
|
||||
"passengerID": box.read(BoxName.passengerID).toString(),
|
||||
});
|
||||
Get.defaultDialog(
|
||||
title: 'Invitation Used'
|
||||
.tr, // Automatically translates based on the current locale
|
||||
middleText: "Your invite code was successfully applied!"
|
||||
.tr, // Automatically translates based on the current locale
|
||||
onConfirm: () {
|
||||
try {
|
||||
CRUD().post(link: AppLink.addPassengersPromo, payload: {
|
||||
"promoCode":
|
||||
'S-${(box.read(BoxName.name)).toString().split(' ')[0]}',
|
||||
"amount": '25',
|
||||
"passengerID": box.read(BoxName.passengerID).toString(),
|
||||
"description": 'promo first'
|
||||
});
|
||||
} catch (e) {
|
||||
addError(e.toString(),
|
||||
'passenger Invitation Used dialogu as promo line 185 login_controller');
|
||||
} finally {
|
||||
// Continue with the rest of your flow, regardless of errors
|
||||
// For example, navigate to the next page
|
||||
Get.offAll(() => const MapPagePassenger());
|
||||
}
|
||||
},
|
||||
textConfirm: "OK".tr, // Confirm button text
|
||||
);
|
||||
} else {
|
||||
Get.offAll(() => const MapPagePassenger());
|
||||
}
|
||||
} else {
|
||||
Get.offAll(() => SmsSignupEgypt());
|
||||
// Get.snackbar(jsonDecoeded['status'], jsonDecoeded['data'],
|
||||
// backgroundColor: Colors.redAccent);
|
||||
isloading = false;
|
||||
update();
|
||||
}
|
||||
} else {
|
||||
isloading = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool?> _confirmDeviceChangeDialog() {
|
||||
return Get.defaultDialog<bool>(
|
||||
barrierDismissible: false,
|
||||
title: 'Device Change Detected'.tr,
|
||||
middleText: 'Please verify your identity'.tr,
|
||||
textConfirm: 'Verify'.tr,
|
||||
confirmTextColor: Colors.white,
|
||||
onConfirm: () => Get.back(result: true),
|
||||
textCancel: 'Cancel'.tr,
|
||||
onCancel: () => Get.back(result: false),
|
||||
);
|
||||
}
|
||||
|
||||
void login() async {
|
||||
isloading = true;
|
||||
update();
|
||||
|
||||
Reference in New Issue
Block a user