206 lines
7.0 KiB
Dart
206 lines
7.0 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:Tripz/constant/box_name.dart';
|
|
import 'package:Tripz/controller/auth/login_controller.dart';
|
|
import 'package:Tripz/controller/functions/encrypt_decrypt.dart';
|
|
import 'package:Tripz/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, EncryptionHelper.instance.encryptData(user.email));
|
|
box.write(BoxName.name,
|
|
EncryptionHelper.instance.encryptData(user.displayName.toString()));
|
|
box.write(BoxName.passengerPhotoUrl,
|
|
EncryptionHelper.instance.encryptData(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.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);
|
|
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
|
|
}
|
|
}
|