25-7-26-1
This commit is contained in:
37
lib/controller/auth/apple_signin_controller.dart
Normal file
37
lib/controller/auth/apple_signin_controller.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
|
||||
|
||||
class AuthController extends GetxController {
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
|
||||
Future<User?> signInWithApple() async {
|
||||
try {
|
||||
final appleCredential = await SignInWithApple.getAppleIDCredential(
|
||||
scopes: [
|
||||
AppleIDAuthorizationScopes.email,
|
||||
AppleIDAuthorizationScopes.fullName,
|
||||
],
|
||||
);
|
||||
|
||||
final oAuthProvider = OAuthProvider('apple.com');
|
||||
final credential = oAuthProvider.credential(
|
||||
idToken: appleCredential.identityToken,
|
||||
accessToken: appleCredential.authorizationCode,
|
||||
);
|
||||
|
||||
UserCredential userCredential =
|
||||
await _auth.signInWithCredential(credential);
|
||||
|
||||
return userCredential.user;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signOut() async {
|
||||
try {
|
||||
await _auth.signOut();
|
||||
} catch (error) {}
|
||||
}
|
||||
}
|
||||
204
lib/controller/auth/google_sign.dart
Normal file
204
lib/controller/auth/google_sign.dart
Normal file
@@ -0,0 +1,204 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:Intaleq/constant/box_name.dart';
|
||||
import 'package:Intaleq/controller/auth/login_controller.dart';
|
||||
import 'package:Intaleq/controller/functions/encrypt_decrypt.dart';
|
||||
import 'package:Intaleq/main.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../../constant/links.dart';
|
||||
import '../../onbording_page.dart';
|
||||
import '../../print.dart';
|
||||
import '../functions/crud.dart';
|
||||
|
||||
class GoogleSignInHelper {
|
||||
static final GoogleSignIn _googleSignIn = GoogleSignIn(
|
||||
scopes: [
|
||||
'email',
|
||||
'profile',
|
||||
],
|
||||
);
|
||||
|
||||
// Method to handle Google Sign-In
|
||||
static Future<GoogleSignInAccount?> signIn() async {
|
||||
try {
|
||||
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
|
||||
if (googleUser != null) {
|
||||
await _handleSignUp(googleUser);
|
||||
// if (box.read(BoxName.countryCode) == 'Egypt') {
|
||||
// Get.to(() => EgyptCardAI());
|
||||
// } else if (box.read(BoxName.countryCode) == 'Jordan') {
|
||||
// Get.to(() => AiPage());
|
||||
// }
|
||||
}
|
||||
return googleUser;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<GoogleSignInAccount?> signInFromLogin() async {
|
||||
try {
|
||||
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
|
||||
if (googleUser != null) {
|
||||
await _handleSignUp(googleUser);
|
||||
await Get.put(LoginController()).loginUsingCredentials(
|
||||
box.read(BoxName.passengerID).toString(),
|
||||
box.read(BoxName.email).toString(),
|
||||
);
|
||||
}
|
||||
return googleUser;
|
||||
} catch (error) {
|
||||
// if (error is GoogleSignInAuthenticationException) {
|
||||
// // Handle authentication errors from Google Sign-In
|
||||
// addError("Google sign-in authentication error: ${error.message}",
|
||||
// '<GoogleSignInAccount?> signInFromLogin()');
|
||||
// } else if (error is GoogleSignInAccountNotFoundException) {
|
||||
// // Handle the case where the user is not found (if applicable)
|
||||
// addError("Google sign-in account not found error: ${error.message}",
|
||||
// '<GoogleSignInAccount?> signInFromLogin()');
|
||||
// }
|
||||
// else
|
||||
if (error is SocketException) {
|
||||
// Handle network issues, like SSL certificate issues
|
||||
addError("Network error (SSL certificate issue): ${error.message}",
|
||||
'<GoogleSignInAccount?> signInFromLogin()');
|
||||
} else if (error is PlatformException) {
|
||||
// Handle platform-specific errors, like Google Play Services issues
|
||||
if (error.code == 'sign_in_required') {
|
||||
// Google Play Services are required but not installed or outdated
|
||||
showGooglePlayServicesError();
|
||||
} else {
|
||||
addError("Platform error: ${error.message}",
|
||||
'<GoogleSignInAccount?> signInFromLogin()');
|
||||
}
|
||||
} else {
|
||||
// Catch all other unknown errors
|
||||
addError("Unknown error: ${error.toString()}",
|
||||
'<GoogleSignInAccount?> signInFromLogin()');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void showGooglePlayServicesError() async {
|
||||
const playStoreUrl =
|
||||
'https://play.google.com/store/apps/details?id=com.google.android.gms&hl=en_US';
|
||||
|
||||
if (await canLaunchUrl(Uri.parse(playStoreUrl))) {
|
||||
await launchUrl(Uri.parse(playStoreUrl));
|
||||
} else {
|
||||
// Fallback if the URL can't be opened
|
||||
showDialog(
|
||||
context: Get.context!,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text('Error'.tr),
|
||||
content: Text(
|
||||
'Could not open the Google Play Store. Please update Google Play Services manually.'
|
||||
.tr),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text('Close'.tr),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Future<GoogleSignInAccount?> signInFromLogin() async {
|
||||
// try {
|
||||
// final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
|
||||
// if (googleUser != null) {
|
||||
// await _handleSignUp(googleUser);
|
||||
// // if (box.read(BoxName.countryCode) == 'Egypt') {
|
||||
// await Get.put(LoginController()).loginUsingCredentials(
|
||||
// box.read(BoxName.passengerID).toString(),
|
||||
// box.read(BoxName.email).toString(),
|
||||
// );
|
||||
// // } else if (box.read(BoxName.countryCode) == 'Jordan') {
|
||||
// // // Get.to(() => AiPage());
|
||||
// // }
|
||||
// }
|
||||
// return googleUser;
|
||||
// } catch (error) {
|
||||
// addError(error.toString(), '<GoogleSignInAccount?> signInFromLogin()');
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
|
||||
addError(String error, where) async {
|
||||
CRUD().post(link: AppLink.addError, payload: {
|
||||
'error': error.toString(), // Example error description
|
||||
'userId': box.read(BoxName.driverID) ??
|
||||
box.read(BoxName.passengerID), // Example user ID
|
||||
'userType': box.read(BoxName.driverID) != null
|
||||
? 'Driver'
|
||||
: 'passenger', // Example user type
|
||||
'phone': box.read(BoxName.phone) ??
|
||||
box.read(BoxName.phoneDriver), // Example phone number
|
||||
|
||||
'device': where
|
||||
});
|
||||
}
|
||||
|
||||
// Method to handle Google Sign-Out
|
||||
static Future<void> signOut() async {
|
||||
try {
|
||||
await _googleSignIn.signOut();
|
||||
await _handleSignOut();
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
// Method to get the current signed-in user
|
||||
static GoogleSignInAccount? getCurrentUser() {
|
||||
return _googleSignIn.currentUser;
|
||||
}
|
||||
|
||||
// Method to handle sign-up process
|
||||
static Future<void> _handleSignUp(GoogleSignInAccount user) async {
|
||||
// Store driver information
|
||||
|
||||
box.write(BoxName.passengerID, user.id);
|
||||
box.write(BoxName.email, (user.email));
|
||||
box.write(BoxName.name, (user.displayName.toString()));
|
||||
box.write(BoxName.passengerPhotoUrl, (user.photoUrl.toString()));
|
||||
|
||||
// Perform any additional sign-up tasks or API calls here
|
||||
// For example, you can send the user data to your server for registration
|
||||
}
|
||||
|
||||
// Method to handle sign-out process
|
||||
static Future<void> _handleSignOut() async {
|
||||
// Clear stored driver information
|
||||
box.erase();
|
||||
// box.remove(BoxName.passengerPhotoUrl);
|
||||
// box.remove(BoxName.driverID);
|
||||
// box.remove(BoxName.email);
|
||||
// box.remove(BoxName.lang);
|
||||
// box.remove(BoxName.name);
|
||||
// box.remove(BoxName.passengerID);
|
||||
// box.remove(BoxName.phone);
|
||||
// box.remove(BoxName.tokenFCM);
|
||||
// box.remove(BoxName.tokens);
|
||||
// box.remove(BoxName.addHome);
|
||||
// box.remove(BoxName.addWork);
|
||||
// box.remove(BoxName.agreeTerms);
|
||||
// box.remove(BoxName.apiKeyRun);
|
||||
// box.remove(BoxName.countryCode);
|
||||
// box.remove(BoxName.accountIdStripeConnect);
|
||||
// box.remove(BoxName.passengerWalletTotal);
|
||||
// box.remove(BoxName.isVerified);
|
||||
// box.remove(BoxName.firstTimeLoadKey);
|
||||
Get.offAll(() => OnBoardingPage());
|
||||
// Perform any additional sign-out tasks or API calls here
|
||||
// For example, you can notify your server about the user sign-out
|
||||
}
|
||||
}
|
||||
406
lib/controller/auth/login_controller.dart
Normal file
406
lib/controller/auth/login_controller.dart
Normal file
@@ -0,0 +1,406 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:Intaleq/constant/api_key.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'package:Intaleq/constant/info.dart';
|
||||
import 'package:Intaleq/controller/functions/add_error.dart';
|
||||
import 'package:Intaleq/views/auth/login_page.dart';
|
||||
import 'package:Intaleq/views/auth/sms_verfy_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:Intaleq/constant/box_name.dart';
|
||||
import 'package:Intaleq/constant/links.dart';
|
||||
import 'package:Intaleq/controller/functions/crud.dart';
|
||||
import 'package:Intaleq/main.dart';
|
||||
import 'package:Intaleq/views/home/map_page_passenger.dart';
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
import 'package:location/location.dart';
|
||||
import 'package:secure_string_operations/secure_string_operations.dart';
|
||||
|
||||
import '../../constant/char_map.dart';
|
||||
import '../../print.dart';
|
||||
import '../../views/auth/otp_token_page.dart';
|
||||
import '../functions/encrypt_decrypt.dart';
|
||||
import '../functions/package_info.dart';
|
||||
import '../functions/secure_storage.dart';
|
||||
import '../functions/securty_check.dart';
|
||||
|
||||
class LoginController extends GetxController {
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final formKeyAdmin = GlobalKey<FormState>();
|
||||
TextEditingController emailController = TextEditingController();
|
||||
TextEditingController phoneController = TextEditingController();
|
||||
TextEditingController passwordController = TextEditingController();
|
||||
TextEditingController adminPasswordController = TextEditingController();
|
||||
TextEditingController adminNameController = TextEditingController();
|
||||
bool isAgreeTerms = false;
|
||||
bool isloading = false;
|
||||
late int isTest = 1;
|
||||
void changeAgreeTerm() {
|
||||
isAgreeTerms = !isAgreeTerms;
|
||||
update();
|
||||
}
|
||||
|
||||
var dev = '';
|
||||
@override
|
||||
void onInit() async {
|
||||
await getAppTester();
|
||||
Log.print('box.read(BoxName.isTest): ${box.read(BoxName.isTest)}');
|
||||
box.write(BoxName.countryCode, 'Syria');
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
getAppTester() async {
|
||||
var res = await CRUD().get(
|
||||
link: AppLink.getTesterApp,
|
||||
payload: {'appPlatform': AppInformation.appName});
|
||||
if (res != 'failure') {
|
||||
var d = jsonDecode(res);
|
||||
|
||||
isTest = int.parse(d['message'][0]['isTest'].toString());
|
||||
box.write(BoxName.isTest, isTest);
|
||||
update();
|
||||
} else {
|
||||
box.write(BoxName.isTest, '1');
|
||||
update();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
updateAppTester(String appPlatform) async {
|
||||
await CRUD().post(
|
||||
link: AppLink.updateTesterApp, payload: {'appPlatform': appPlatform});
|
||||
}
|
||||
|
||||
void saveAgreementTerms() {
|
||||
box.write(BoxName.agreeTerms, 'agreed');
|
||||
update();
|
||||
}
|
||||
|
||||
void saveCountryCode(String countryCode) {
|
||||
box.write(BoxName.countryCode, countryCode);
|
||||
update();
|
||||
}
|
||||
|
||||
getJwtWallet() async {
|
||||
final random = Random();
|
||||
|
||||
if (random.nextBool()) {
|
||||
await SecurityHelper.performSecurityChecks();
|
||||
} else {
|
||||
await SecurityChecks.isDeviceRootedFromNative(Get.context!);
|
||||
}
|
||||
|
||||
String fingerPrint = await DeviceHelper.getDeviceFingerprint();
|
||||
// print('fingerPrint: ${fingerPrint}');
|
||||
dev = Platform.isAndroid ? 'android' : 'ios';
|
||||
var payload = {
|
||||
'id': box.read(BoxName.passengerID),
|
||||
'password': AK.passnpassenger,
|
||||
'aud': '${AK.allowed}$dev',
|
||||
'fingerPrint': fingerPrint
|
||||
};
|
||||
var response1 = await http.post(
|
||||
Uri.parse(AppLink.loginJwtWalletRider),
|
||||
body: payload,
|
||||
);
|
||||
await box.write(BoxName.hmac, jsonDecode(response1.body)['hmac']);
|
||||
// Log.print('jsonDecoeded[hmac]: ${jsonDecoeded['hmac']}');
|
||||
// Log.print('req: ${response1.request}');
|
||||
// Log.print('response: ${response1.body}');
|
||||
// Log.print('payload: ${payload}');
|
||||
return jsonDecode(response1.body)['jwt'].toString();
|
||||
}
|
||||
|
||||
getJWT() async {
|
||||
// print(Pasenger.pasengerpas);
|
||||
// await SecurityHelper.performSecurityChecks();
|
||||
Log.print('firstTimeLoadKey: ${box.read(BoxName.firstTimeLoadKey)}');
|
||||
dev = Platform.isAndroid ? 'android' : 'ios';
|
||||
if (box.read(BoxName.firstTimeLoadKey).toString() != 'false') {
|
||||
var payload = {
|
||||
'id': box.read(BoxName.passengerID) ?? AK.newId,
|
||||
'password': AK.passnpassenger,
|
||||
'aud': '${AK.allowed}$dev',
|
||||
};
|
||||
// Log.print('payload: ${payload}');
|
||||
var response0 = await http.post(
|
||||
Uri.parse(AppLink.loginFirstTime),
|
||||
body: payload,
|
||||
);
|
||||
if (response0.statusCode == 200) {
|
||||
final decodedResponse1 = jsonDecode(response0.body);
|
||||
|
||||
final jwt = decodedResponse1['jwt'];
|
||||
final refreshToken = decodedResponse1['refresh_token'];
|
||||
box.write(BoxName.jwt, c(jwt));
|
||||
// Sss.write(BoxName.jwt, jwt);
|
||||
await storage.write(key: BoxName.refreshToken, value: refreshToken);
|
||||
|
||||
// await AppInitializer().getAIKey(Pasenger.keyOfApp);
|
||||
// await AppInitializer().getAIKey(Pasenger.initializationVector);
|
||||
// await Future.delayed(Duration.zero);
|
||||
await EncryptionHelper.initialize();
|
||||
|
||||
await AppInitializer().getKey();
|
||||
} else {}
|
||||
} else {
|
||||
await EncryptionHelper.initialize();
|
||||
|
||||
var payload = {
|
||||
'id': box.read(BoxName.passengerID),
|
||||
'password': box.read(BoxName.email),
|
||||
'aud': '${AK.allowed}$dev',
|
||||
};
|
||||
// Log.print('payload: ${payload}');
|
||||
var response1 = await http.post(
|
||||
Uri.parse(AppLink.loginJwtRider),
|
||||
body: payload,
|
||||
);
|
||||
// Log.print('req: ${response1.request}');
|
||||
// Log.print('response: ${response1.body}');
|
||||
// Log.print('payload: ${payload}');
|
||||
// Log.print('decodedResponse1: ${jsonDecode(response1.body)}');
|
||||
|
||||
if (response1.statusCode == 200) {
|
||||
final decodedResponse1 = jsonDecode(response1.body);
|
||||
// Log.print('decodedResponse1: ${decodedResponse1}');
|
||||
|
||||
final jwt = decodedResponse1['jwt'];
|
||||
await box.write(BoxName.jwt, c(jwt));
|
||||
|
||||
await AppInitializer().getKey();
|
||||
|
||||
final refreshToken = decodedResponse1['refresh_token'];
|
||||
await storage.write(key: BoxName.refreshToken, value: refreshToken);
|
||||
} else {}
|
||||
}
|
||||
}
|
||||
|
||||
loginUsingCredentials(String passengerID, email) async {
|
||||
isloading = true;
|
||||
update();
|
||||
bool isTokenExpired = JwtDecoder.isExpired(
|
||||
r(box.read(BoxName.jwt)).toString().split(AppInformation.addd)[0]);
|
||||
|
||||
if (isTokenExpired) {
|
||||
// Log.print('isTokenExpired loginUsingCredentials: ${isTokenExpired}');
|
||||
await getJWT();
|
||||
}
|
||||
|
||||
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());
|
||||
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(
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void login() async {
|
||||
isloading = true;
|
||||
update();
|
||||
var res =
|
||||
await CRUD().get(link: AppLink.loginFromGooglePassenger, payload: {
|
||||
'email': (emailController.text),
|
||||
'id': passwordController.text,
|
||||
"platform": Platform.isAndroid ? 'android' : 'ios',
|
||||
"appName": AppInformation.appName,
|
||||
});
|
||||
|
||||
isloading = false;
|
||||
update();
|
||||
if (res == 'Failure') {
|
||||
//Failure
|
||||
Get.offAll(() => LoginPage());
|
||||
isloading = false;
|
||||
update();
|
||||
// Get.snackbar("User does not exist.".tr, '', backgroundColor: Colors.red);
|
||||
} else {
|
||||
var jsonDecoeded = jsonDecode(res);
|
||||
if (jsonDecoeded.isNotEmpty) {
|
||||
if (jsonDecoeded['status'] == 'success' &&
|
||||
jsonDecoeded['data'][0]['verified'].toString() == '1') {
|
||||
//
|
||||
box.write(BoxName.isVerified, '1');
|
||||
box.write(BoxName.email, jsonDecoeded['data'][0]['email']);
|
||||
box.write(BoxName.name, jsonDecoeded['data'][0]['first_name']);
|
||||
box.write(BoxName.phone, jsonDecoeded['data'][0]['phone']);
|
||||
box.write(BoxName.passengerID, passwordController.text);
|
||||
// var token = await CRUD().get(link: AppLink.getTokens, payload: {
|
||||
// 'passengerID': box.read(BoxName.passengerID).toString()
|
||||
// });
|
||||
await updateAppTester(AppInformation.appName);
|
||||
|
||||
Get.offAll(() => const MapPagePassenger());
|
||||
} else {
|
||||
// Get.offAll(() => SmsSignupEgypt());
|
||||
// Get.snackbar(jsonDecoeded['status'], jsonDecoeded['data'],
|
||||
// backgroundColor: Colors.redAccent);
|
||||
isloading = false;
|
||||
update();
|
||||
}
|
||||
} else {
|
||||
isloading = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
goToMapPage() {
|
||||
if (box.read(BoxName.email) != null) {
|
||||
Get.offAll(() => const MapPagePassenger());
|
||||
}
|
||||
}
|
||||
|
||||
final location = Location();
|
||||
|
||||
// late PermissionStatus permissionGranted = PermissionStatus.denied;
|
||||
Future<void> getLocationPermission() async {
|
||||
bool serviceEnabled;
|
||||
PermissionStatus permissionGranted;
|
||||
|
||||
// Check if location services are enabled
|
||||
serviceEnabled = await location.serviceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
serviceEnabled = await location.requestService();
|
||||
if (!serviceEnabled) {
|
||||
// Location services are still not enabled, handle the error
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the app has permission to access location
|
||||
permissionGranted = await location.hasPermission();
|
||||
if (permissionGranted == PermissionStatus.denied) {
|
||||
permissionGranted = await location.requestPermission();
|
||||
if (permissionGranted != PermissionStatus.granted) {
|
||||
// Location permission is still not granted, handle the error
|
||||
permissionGranted = await location.requestPermission();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (permissionGranted.toString() == 'PermissionStatus.granted') {
|
||||
box.write(BoxName.locationPermission, 'true');
|
||||
}
|
||||
update();
|
||||
}
|
||||
}
|
||||
43
lib/controller/auth/onboarding_controller.dart
Normal file
43
lib/controller/auth/onboarding_controller.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:Intaleq/constant/box_name.dart';
|
||||
import 'package:Intaleq/main.dart';
|
||||
import 'package:Intaleq/views/auth/login_page.dart';
|
||||
|
||||
import '../../models/model/onboarding_model.dart';
|
||||
|
||||
abstract class OnBoardingController extends GetxController {
|
||||
next();
|
||||
onPageChanged(int index);
|
||||
}
|
||||
|
||||
class OnBoardingControllerImp extends OnBoardingController {
|
||||
late PageController pageController;
|
||||
|
||||
int currentPage = 0;
|
||||
|
||||
@override
|
||||
next() {
|
||||
currentPage++;
|
||||
|
||||
if (currentPage > onBoardingList.length - 1) {
|
||||
box.write(BoxName.onBoarding, 'yes');
|
||||
Get.offAll(() => LoginPage());
|
||||
} else {
|
||||
pageController.animateToPage(currentPage,
|
||||
duration: const Duration(milliseconds: 900), curve: Curves.easeInOut);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
onPageChanged(int index) {
|
||||
currentPage = index;
|
||||
update();
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
pageController = PageController();
|
||||
super.onInit();
|
||||
}
|
||||
}
|
||||
133
lib/controller/auth/otp_controller.dart
Normal file
133
lib/controller/auth/otp_controller.dart
Normal file
@@ -0,0 +1,133 @@
|
||||
import 'package:Intaleq/constant/links.dart';
|
||||
import 'package:Intaleq/views/home/map_page_passenger.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../../../constant/box_name.dart';
|
||||
import '../../../main.dart';
|
||||
import '../../../print.dart';
|
||||
import '../../views/auth/otp_page.dart';
|
||||
import '../../views/widgets/error_snakbar.dart';
|
||||
import '../functions/crud.dart';
|
||||
import 'login_controller.dart';
|
||||
|
||||
// --- Helper Class for Phone Authentication ---
|
||||
|
||||
class PhoneAuthHelper {
|
||||
// Define your server URLs
|
||||
static final String _baseUrl = '${AppLink.server}/auth/syria/';
|
||||
static final String _sendOtpUrl = '${_baseUrl}sendWhatsOpt.php';
|
||||
static final String _verifyOtpUrl = '${_baseUrl}verifyOtp.php';
|
||||
static final String _registerUrl = '${_baseUrl}register_passenger.php';
|
||||
|
||||
/// Sends an OTP to the provided phone number.
|
||||
static Future<bool> sendOtp(String phoneNumber) async {
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: _sendOtpUrl,
|
||||
payload: {'receiver': phoneNumber},
|
||||
);
|
||||
// Log.print('response: ${response}');
|
||||
if (response != 'failure') {
|
||||
final data = (response);
|
||||
// if (data['status'] == 'success') {
|
||||
mySnackbarSuccess('An OTP has been sent to your WhatsApp 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) {
|
||||
Log.print('e: ${e}');
|
||||
// mySnackeBarError('An error occurred: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Verifies the OTP and logs the user in.
|
||||
|
||||
static Future<void> verifyOtp(String phoneNumber, String otp) async {
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: _verifyOtpUrl,
|
||||
payload: {'phone_number': phoneNumber, 'otp': otp},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
final data = (response);
|
||||
// Log.print('data: ${data}');
|
||||
|
||||
if (data['status'] == 'success') {
|
||||
final isRegistered = data['message']['isRegistered'] ?? false;
|
||||
// Log.print('isRegistered: ${isRegistered}');
|
||||
|
||||
if (isRegistered) {
|
||||
// ✅ المستخدم موجود مسبقاً -> تسجيل دخول مباشر
|
||||
await _handleSuccessfulLogin(data['message']['passenger']);
|
||||
} else {
|
||||
// ✅ مستخدم جديد -> الذهاب لصفحة التسجيل
|
||||
mySnackbarSuccess(
|
||||
'Phone verified. Please complete registration.'.tr);
|
||||
Get.to(() => RegistrationScreen(phoneNumber: phoneNumber));
|
||||
}
|
||||
} else {
|
||||
mySnackeBarError(data['message']);
|
||||
}
|
||||
} else {
|
||||
mySnackeBarError('Server error. Please try again.'.tr);
|
||||
}
|
||||
} 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']['data']);
|
||||
} else {
|
||||
mySnackeBarError(
|
||||
"User with this phone number or email already exists.".tr);
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('e: ${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.name, userData['first_name']);
|
||||
box.write(BoxName.lastName, userData['last_name']);
|
||||
box.write(BoxName.email, userData['email']);
|
||||
box.write(BoxName.phone, userData['phone']);
|
||||
box.write(BoxName.isVerified, '1');
|
||||
|
||||
await Get.put(LoginController()).loginUsingCredentials(
|
||||
box.read(BoxName.passengerID).toString(),
|
||||
box.read(BoxName.email).toString(),
|
||||
); // Navigate to home
|
||||
}
|
||||
}
|
||||
381
lib/controller/auth/register_controller.dart
Normal file
381
lib/controller/auth/register_controller.dart
Normal file
@@ -0,0 +1,381 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:Intaleq/constant/colors.dart';
|
||||
import 'package:Intaleq/controller/auth/login_controller.dart';
|
||||
import 'package:Intaleq/controller/functions/add_error.dart';
|
||||
import 'package:Intaleq/controller/functions/encrypt_decrypt.dart';
|
||||
import 'package:Intaleq/views/home/map_page_passenger.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:Intaleq/constant/links.dart';
|
||||
import 'package:Intaleq/constant/style.dart';
|
||||
import 'package:Intaleq/controller/functions/crud.dart';
|
||||
import 'package:Intaleq/views/auth/login_page.dart';
|
||||
import 'package:Intaleq/views/widgets/elevated_btn.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../../views/auth/verify_email_page.dart';
|
||||
import '../../views/widgets/mydialoug.dart';
|
||||
import '../functions/sms_controller.dart';
|
||||
|
||||
class RegisterController extends GetxController {
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final formKey3 = GlobalKey<FormState>();
|
||||
|
||||
TextEditingController firstNameController = TextEditingController();
|
||||
TextEditingController lastNameController = TextEditingController();
|
||||
TextEditingController emailController = TextEditingController();
|
||||
TextEditingController phoneController = TextEditingController();
|
||||
TextEditingController passwordController = TextEditingController();
|
||||
TextEditingController siteController = TextEditingController();
|
||||
// TextEditingController verfyCode = TextEditingController();
|
||||
TextEditingController verifyCode = TextEditingController();
|
||||
int remainingTime = 300; // 5 minutes in seconds
|
||||
bool isSent = false;
|
||||
bool isLoading = false;
|
||||
Timer? _timer;
|
||||
String birthDate = 'Birth Date'.tr;
|
||||
String gender = 'Male'.tr;
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
void startTimer() {
|
||||
_timer?.cancel(); // Cancel any existing timer
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (remainingTime > 0) {
|
||||
remainingTime--;
|
||||
} else {
|
||||
timer.cancel();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getBirthDate() {
|
||||
Get.defaultDialog(
|
||||
title: 'Select Date'.tr,
|
||||
titleStyle: AppStyle.title,
|
||||
content: SizedBox(
|
||||
width: 300,
|
||||
child: CalendarDatePicker(
|
||||
initialDate:
|
||||
DateTime.now().subtract(const Duration(days: 14 * 365)),
|
||||
firstDate: DateTime.parse('1940-06-01'),
|
||||
lastDate: DateTime.now().subtract(const Duration(days: 14 * 365)),
|
||||
onDateChanged: (date) {
|
||||
// Get the selected date and convert it to a DateTime object
|
||||
DateTime dateTime = date;
|
||||
// Call the getOrders() function from the controller
|
||||
birthDate = dateTime.toString().split(' ')[0];
|
||||
update();
|
||||
},
|
||||
|
||||
// onDateChanged: (DateTime value) {},
|
||||
),
|
||||
),
|
||||
confirm: MyElevatedButton(title: 'Ok'.tr, onPressed: () => Get.back()));
|
||||
}
|
||||
|
||||
void changeGender(String value) {
|
||||
gender = value;
|
||||
update();
|
||||
}
|
||||
|
||||
bool isValidEgyptianPhoneNumber(String phoneNumber) {
|
||||
// Remove any whitespace from the phone number
|
||||
phoneNumber = phoneNumber.replaceAll(RegExp(r'\s+'), '');
|
||||
|
||||
// Check if the phone number has exactly 11 digits
|
||||
if (phoneNumber.length != 11) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if the phone number starts with 010, 011, 012, or 015
|
||||
RegExp validPrefixes = RegExp(r'^01[0125]\d{8}$');
|
||||
|
||||
return validPrefixes.hasMatch(phoneNumber);
|
||||
}
|
||||
|
||||
bool isValidPhoneNumber(String phoneNumber) {
|
||||
// Remove any whitespace from the phone number
|
||||
phoneNumber = phoneNumber.replaceAll(RegExp(r'\s+'), '');
|
||||
|
||||
// Check if the phone number has at least 10 digits
|
||||
if (phoneNumber.length < 10) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for valid prefixes (modify this to match your use case)
|
||||
RegExp validPrefixes = RegExp(r'^[0-9]+$');
|
||||
|
||||
// Check if the phone number contains only digits
|
||||
return validPrefixes.hasMatch(phoneNumber);
|
||||
}
|
||||
|
||||
sendOtpMessage() async {
|
||||
SmsEgyptController smsEgyptController;
|
||||
isLoading = true;
|
||||
update();
|
||||
try {
|
||||
// Initialize SmsEgyptController
|
||||
smsEgyptController = Get.put(SmsEgyptController());
|
||||
|
||||
isLoading = true;
|
||||
update();
|
||||
|
||||
// Get phone number from controller
|
||||
String phoneNumber = phoneController.text;
|
||||
|
||||
// Check if the phone number is from Egypt (Assuming Egyptian numbers start with +20)
|
||||
|
||||
if (phoneController.text.isNotEmpty) {
|
||||
bool isEgyptianNumber = phoneNumber.startsWith('+20');
|
||||
if (isEgyptianNumber && phoneNumber.length == 13) {
|
||||
// Check if the phone number is already verified
|
||||
var responseChecker = await CRUD().post(
|
||||
link: AppLink.checkPhoneNumberISVerfiedPassenger,
|
||||
payload: {
|
||||
'phone_number': (phoneNumber),
|
||||
'email': box.read(BoxName.email),
|
||||
},
|
||||
);
|
||||
|
||||
if (responseChecker != 'failure') {
|
||||
var data = jsonDecode(responseChecker);
|
||||
|
||||
// If the phone number is already verified
|
||||
if (data['message'][0]['verified'].toString() == '1') {
|
||||
Get.snackbar('Phone number is verified before'.tr, '',
|
||||
backgroundColor: AppColor.greenColor);
|
||||
box.write(BoxName.isVerified, '1');
|
||||
box.write(BoxName.phone, (phoneNumber));
|
||||
Get.offAll(const MapPagePassenger());
|
||||
} else {
|
||||
await sendOtp(phoneNumber, isEgyptianNumber, smsEgyptController);
|
||||
}
|
||||
} else {
|
||||
await sendOtp(phoneNumber, isEgyptianNumber, smsEgyptController);
|
||||
}
|
||||
} else if (phoneNumber.length > 9) {
|
||||
sendOtp(phoneNumber, isEgyptianNumber, smsEgyptController);
|
||||
}
|
||||
} else {
|
||||
MyDialog().getDialog(
|
||||
'Error'.tr, 'Phone number must be exactly 11 digits long'.tr, () {
|
||||
Get.back();
|
||||
});
|
||||
// sendOtp(
|
||||
// phoneNumber, randomNumber, isEgyptianNumber, smsEgyptController);
|
||||
}
|
||||
} catch (e) {
|
||||
// Handle error
|
||||
} finally {
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to send OTP or WhatsApp message based on phone number location
|
||||
Future<void> sendOtp(String phoneNumber, bool isEgyptian,
|
||||
SmsEgyptController controller) async {
|
||||
// Trim any leading or trailing whitespace from the phone number
|
||||
phoneNumber = phoneNumber.trim();
|
||||
var dd = await CRUD().post(link: AppLink.sendVerifyOtpMessage, payload: {
|
||||
'phone_number': (phoneNumber),
|
||||
});
|
||||
Log.print('dd: ${dd}');
|
||||
|
||||
// Common Registration Logic (extracted for reuse)
|
||||
Future<void> registerUser() async {
|
||||
await CRUD().post(link: AppLink.updatePhoneInvalidSMSPassenger, payload: {
|
||||
"phone_number": (Get.find<RegisterController>().phoneController.text)
|
||||
});
|
||||
|
||||
box.write(BoxName.phone, (phoneController.text));
|
||||
|
||||
var nameParts = (box.read(BoxName.name)).toString().split(' ');
|
||||
var firstName = nameParts.isNotEmpty ? nameParts[0] : 'unknown';
|
||||
var lastName = nameParts.length > 1 ? nameParts[1] : 'unknown';
|
||||
|
||||
var payload = {
|
||||
'id': box.read(BoxName.passengerID),
|
||||
'phone': (phoneController.text),
|
||||
'email': box.read(BoxName.email),
|
||||
'password':
|
||||
('unknown'), //Consider if you *really* want to store 'unknown' passwords
|
||||
'gender': ('unknown'),
|
||||
'birthdate': ('2002-01-01'),
|
||||
'site': box.read(BoxName.passengerPhotoUrl) ?? 'unknown',
|
||||
'first_name': (firstName),
|
||||
'last_name': (lastName),
|
||||
};
|
||||
|
||||
var res1 = await CRUD().post(
|
||||
link: AppLink.signUp,
|
||||
payload: payload,
|
||||
);
|
||||
|
||||
if (res1 != 'failure') {
|
||||
//Multi-server signup (moved inside the successful registration check)
|
||||
if (AppLink.IntaleqAlexandriaServer != AppLink.IntaleqCairoServer) {
|
||||
List<Future> signUp = [
|
||||
CRUD().post(
|
||||
link: '${AppLink.IntaleqAlexandriaServer}/auth/signup.php',
|
||||
payload: payload,
|
||||
),
|
||||
CRUD().post(
|
||||
link: '${AppLink.IntaleqGizaServer}/auth/signup.php',
|
||||
payload: payload,
|
||||
)
|
||||
];
|
||||
await Future.wait(signUp); // Wait for both sign-ups to complete.
|
||||
}
|
||||
|
||||
box.write(BoxName.isVerified, '1');
|
||||
box.write(
|
||||
BoxName.isFirstTime, '0'); //Double-check the logic for isFirstTime
|
||||
box.write(BoxName.phone, (phoneController.text));
|
||||
|
||||
Get.put(LoginController()).loginUsingCredentials(
|
||||
box.read(BoxName.passengerID).toString(),
|
||||
box.read(BoxName.email).toString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (isEgyptian) {
|
||||
// verifySMSCode();
|
||||
// await registerUser(); // Use the common registration logic
|
||||
// await controller.sendSmsEgypt(phoneNumber, otp.toString()); // Optional: Send SMS if Egyptian
|
||||
} else if (phoneController.text.toString().length >= 10) {
|
||||
await registerUser(); // Use the common registration logic for non-Egyptian users as well.
|
||||
// this for whatsapp messsage // Optional: Send WhatsApp message
|
||||
// await CRUD().sendWhatsAppAuth(phoneNumber, otp.toString());
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
isSent = true;
|
||||
remainingTime = 300;
|
||||
update(); // Reset to 5 minutes
|
||||
// startTimer(); // Consider whether you need a timer here, or if it's handled elsewhere.
|
||||
}
|
||||
|
||||
verifySMSCode() async {
|
||||
try {
|
||||
if (formKey3.currentState!.validate()) {
|
||||
var res = await CRUD().post(link: AppLink.verifyOtpPassenger, payload: {
|
||||
'phone_number': phoneController.text,
|
||||
'token': verifyCode.text,
|
||||
});
|
||||
|
||||
if (res != 'failure') {
|
||||
box.write(BoxName.phone, (phoneController.text));
|
||||
var nameParts = (box.read(BoxName.name)).toString().split(' ');
|
||||
var firstName = nameParts.isNotEmpty ? nameParts[0] : 'unknown';
|
||||
var lastName = nameParts.length > 1 ? nameParts[1] : 'unknown';
|
||||
|
||||
var payload = {
|
||||
'id': box.read(BoxName.passengerID),
|
||||
'phone': (phoneController.text),
|
||||
'email': box.read(BoxName.email),
|
||||
'password': 'unknown',
|
||||
'gender': 'unknown',
|
||||
'birthdate': '2002-01-01',
|
||||
'site': box.read(BoxName.passengerPhotoUrl) ?? 'unknown',
|
||||
'first_name': firstName,
|
||||
'last_name': lastName,
|
||||
};
|
||||
|
||||
var res1 = await CRUD().post(
|
||||
link: AppLink.signUp,
|
||||
payload: payload,
|
||||
);
|
||||
|
||||
if (res1 != 'failure') {
|
||||
if (AppLink.IntaleqAlexandriaServer != AppLink.IntaleqCairoServer) {
|
||||
List<Future> signUp = [
|
||||
CRUD().post(
|
||||
link: '${AppLink.IntaleqAlexandriaServer}/auth/signup.php',
|
||||
payload: payload,
|
||||
),
|
||||
CRUD().post(
|
||||
link: '${AppLink.IntaleqGizaServer}/auth/signup.php',
|
||||
payload: payload,
|
||||
)
|
||||
];
|
||||
await Future.wait(signUp);
|
||||
}
|
||||
|
||||
box.write(BoxName.isVerified, '1');
|
||||
box.write(BoxName.isFirstTime, '0');
|
||||
box.write(BoxName.phone, (phoneController.text));
|
||||
|
||||
Get.put(LoginController()).loginUsingCredentials(
|
||||
box.read(BoxName.passengerID).toString(),
|
||||
box.read(BoxName.email).toString(),
|
||||
);
|
||||
} else {
|
||||
Get.snackbar('Error'.tr,
|
||||
"The email or phone number is already registered.".tr,
|
||||
backgroundColor: Colors.redAccent);
|
||||
}
|
||||
} else {
|
||||
Get.snackbar('Error'.tr, "phone not verified".tr,
|
||||
backgroundColor: Colors.redAccent);
|
||||
}
|
||||
} else {
|
||||
Get.snackbar('Error'.tr, "you must insert token code".tr,
|
||||
backgroundColor: AppColor.redColor);
|
||||
}
|
||||
} catch (e) {
|
||||
addError(e.toString(), 'passenger sign up ');
|
||||
Get.snackbar('Error'.tr, "Something went wrong. Please try again.".tr,
|
||||
backgroundColor: Colors.redAccent);
|
||||
}
|
||||
}
|
||||
|
||||
sendVerifications() async {
|
||||
var res = await CRUD().post(link: AppLink.verifyEmail, payload: {
|
||||
'email': emailController.text,
|
||||
'token': verifyCode.text,
|
||||
});
|
||||
var dec = jsonDecode(res);
|
||||
if (dec['status'] == 'success') {
|
||||
Get.offAll(() => LoginPage());
|
||||
}
|
||||
}
|
||||
|
||||
void register() async {
|
||||
if (formKey.currentState!.validate()) {
|
||||
var res = await CRUD().post(link: AppLink.signUp, payload: {
|
||||
'first_name': firstNameController.text.toString(),
|
||||
'last_name': lastNameController.text.toString(),
|
||||
'email': emailController.text.toString(),
|
||||
'phone': phoneController.text.toString(),
|
||||
'password': passwordController.text.toString(),
|
||||
'gender': 'yet',
|
||||
'site': siteController.text,
|
||||
'birthdate': birthDate,
|
||||
});
|
||||
if (jsonDecode(res)['status'] == 'success') {
|
||||
int randomNumber = Random().nextInt(100000) + 1;
|
||||
await CRUD().post(link: AppLink.sendVerifyEmail, payload: {
|
||||
'email': emailController.text,
|
||||
'token': randomNumber.toString(),
|
||||
});
|
||||
Get.to(() => const VerifyEmailPage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
_timer?.cancel();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
107
lib/controller/auth/token_otp_change_controller.dart
Normal file
107
lib/controller/auth/token_otp_change_controller.dart
Normal file
@@ -0,0 +1,107 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:Intaleq/constant/box_name.dart';
|
||||
import 'package:Intaleq/constant/links.dart';
|
||||
import 'package:Intaleq/controller/functions/crud.dart';
|
||||
import 'package:Intaleq/main.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../../print.dart';
|
||||
import '../../views/home/map_page_passenger.dart';
|
||||
import '../firebase/firbase_messge.dart';
|
||||
|
||||
class OtpVerificationController extends GetxController {
|
||||
final String phone;
|
||||
final String deviceToken;
|
||||
final String token;
|
||||
final otpCode = ''.obs;
|
||||
final isLoading = false.obs;
|
||||
final isVerifying = false.obs;
|
||||
var canResend = false.obs;
|
||||
var countdown = 120.obs;
|
||||
Timer? _timer;
|
||||
|
||||
OtpVerificationController({
|
||||
required this.phone,
|
||||
required this.deviceToken,
|
||||
required this.token,
|
||||
});
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
sendOtp(); // ترسل تلقائيًا عند فتح الصفحة
|
||||
startCountdown();
|
||||
}
|
||||
|
||||
void startCountdown() {
|
||||
canResend.value = false;
|
||||
countdown.value = 120;
|
||||
_timer?.cancel();
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (countdown.value > 0) {
|
||||
countdown.value--;
|
||||
} else {
|
||||
canResend.value = true;
|
||||
timer.cancel();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> sendOtp() async {
|
||||
isLoading.value = true;
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: '${AppLink.server}/auth/token_passenger/send_otp.php',
|
||||
payload: {
|
||||
'receiver': phone,
|
||||
// 'device_token': deviceToken,
|
||||
},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
// بإمكانك عرض رسالة نجاح هنا
|
||||
} else {
|
||||
// Get.snackbar('Error', 'Failed to send OTP');
|
||||
}
|
||||
} catch (e) {
|
||||
Get.snackbar('Error', e.toString());
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> verifyOtp(String ptoken) async {
|
||||
isVerifying.value = true;
|
||||
var finger = await storage.read(key: BoxName.fingerPrint);
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: '${AppLink.server}/auth/token_passenger/verify_otp.php',
|
||||
payload: {
|
||||
'phone_number': phone,
|
||||
'otp': otpCode.value,
|
||||
'token': box.read(BoxName.tokenFCM).toString(),
|
||||
'fingerPrint': finger.toString(),
|
||||
},
|
||||
);
|
||||
|
||||
if (response != 'failure' && response['status'] == 'success') {
|
||||
Get.back(); // توجه إلى الصفحة التالية
|
||||
await Get.put(FirebaseMessagesController()).sendNotificationToDriverMAP(
|
||||
'token change',
|
||||
'change device'.tr,
|
||||
ptoken.toString(),
|
||||
[],
|
||||
'cancel.wav',
|
||||
);
|
||||
Get.offAll(() => const MapPagePassenger());
|
||||
} else {
|
||||
Get.snackbar('Verification Failed', 'OTP is incorrect or expired');
|
||||
}
|
||||
} catch (e) {
|
||||
Get.snackbar('Error', e.toString());
|
||||
} finally {
|
||||
isVerifying.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
38
lib/controller/auth/tokens_controller.dart
Normal file
38
lib/controller/auth/tokens_controller.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../main.dart';
|
||||
import '../functions/encrypt_decrypt.dart';
|
||||
|
||||
class TokenController extends GetxController {
|
||||
bool isloading = false;
|
||||
|
||||
Future addToken() async {
|
||||
String? basicAuthCredentials =
|
||||
await storage.read(key: BoxName.basicAuthCredentials);
|
||||
isloading = true;
|
||||
update();
|
||||
var res = await http.post(
|
||||
Uri.parse(AppLink.addTokens),
|
||||
headers: {
|
||||
'Authorization':
|
||||
'Basic ${base64Encode(utf8.encode(basicAuthCredentials.toString()))}',
|
||||
},
|
||||
body: {
|
||||
'token': (box.read(BoxName.tokenFCM.toString())),
|
||||
'passengerID': box.read(BoxName.passengerID).toString()
|
||||
},
|
||||
);
|
||||
|
||||
isloading = false;
|
||||
update();
|
||||
var jsonToken = jsonDecode(res.body);
|
||||
if (jsonToken['status'] == 'The token has been updated successfully.') {
|
||||
Get.snackbar('token updated'.tr, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
16
lib/controller/auth/verify_email_controller.dart
Normal file
16
lib/controller/auth/verify_email_controller.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:Intaleq/constant/links.dart';
|
||||
import 'package:Intaleq/controller/functions/crud.dart';
|
||||
|
||||
class VerifyEmailController extends GetxController {
|
||||
TextEditingController verfyCode = TextEditingController();
|
||||
@override
|
||||
void onInit() async {
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
sendverfications() async {
|
||||
await CRUD().post(link: AppLink.sendVerifyEmail);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user