handleSignupProvider method
Implementation
Future<void> handleSignupProvider(AuthProvider provider, bool fromSignUp) async {
try {
loadingDialog();
UserCredential userCredential =
await FirebaseAuth.instance.signInWithPopup(provider);
User? currentUser = userCredential.user;
if (currentUser == null) {
closeDialog();
return;
}
var loginResult = await _createUser(currentUser);
if(loginResult == null){
return ;
}
if (loginResult['statusCode'] == 200) {
if (['SUBSCRIBER', 'FOUNDER'].contains(loginResult['role'])) {
closeDialog();
bool didUserCancelDialog = false;
if(loginResult["phone"] == null || loginResult["phone"].toString().isEmpty){
await ResidentCountryView.show(
barrierDismissible: false,
onConfirm: () async {
closeDialog();
},
onPressClose: (){
didUserCancelDialog = true;
closeDialog();
},
);
if(didUserCancelDialog){
return ;
}else{
phoneNumberController.text = mobileNumTxtController.text.replaceAll(countryCodeController.text, "");
bool result =await verifyPhone();
if (!result){
return ;
}
}
}
onLoginSuccessful(referralCode: loginResult['referrerCode']);
} else {
_handleUnauthorizedAccess();
}
} else if (loginResult?['statusCode'] == 400 &&
loginResult?['message'].contains('Invalid referralCode')||loginResult?['message'].contains('Invalid referral code')) {
ReferralCodePopup.show(
onSubmit: (String refCode) async {
final reSigninResult = await Requests.createUser(
currentUser.uid,
firstName: currentUser.displayName?.split(" ")[0] ?? "",
lastName: currentUser.displayName?.split(" ")[1] ?? "",
referralCode: refCode,
email: currentUser.email,
phoneNo: mobileNumTxtController.text.replaceAll(countryCodeController.text, ""),
countryCode: countryCodeController.text,
);
closeDialog();
if (reSigninResult?['statusCode'] == 200 &&
(reSigninResult?['role'] == 'SUBSCRIBER' ||
reSigninResult?['role'] == 'FOUNDER')) {
closeDialog();
if(reSigninResult["phone"] == null || reSigninResult["phone"].toString().isEmpty){
await ResidentCountryView.show(
barrierDismissible: false,
onConfirm: () async {
closeDialog();
},
);
phoneNumberController.text = mobileNumTxtController.text.replaceAll(countryCodeController.text, "");
bool result =await verifyPhone();
if (!result){
return ;
}
}
await onLoginSuccessful(referralCode: reSigninResult?['referrerCode']);
} else {
clearLoginFields();
_handleSignupError();
closeDialog();
}
},
);
} else {
_handleSignupError();
}
} on FirebaseAuthException catch (e) {
closeDialog();
log("FirebaseAuthException: ${e.code}");
if (e.code == 'popup-closed-by-user') {
log("User closed the popup before completing sign-in.");
} else if (e.code == 'account-exists-with-different-credential') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message:
"An account already exists with this email but was created using a different sign-in method. Try signing in using Google, Facebook, or another provider.",
);
} else if (e.code == 'credential-already-in-use') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message:
"This account is already linked with a different provider. Please try signing in with a different method.",
);
} else if (e.code == 'invalid-credential') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid Credentials",
message:
"The credentials provided are invalid or have expired. Please try again.",
);
} else if (e.code == 'user-disabled') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Account Disabled",
message:
"This account has been disabled. Please contact support for assistance.",
);
} else if (e.code == 'too-many-requests') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Too Many Requests",
message:
"Too many attempts have been made from this device. Please try again later.",
);
} else {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Authentication Error",
message: e.message ?? "An error occurred. Please try again.",
);
}
} catch (e) {
closeDialog();
if (!e.toString().contains('popup_closed_by_user')) {
log("Error in sign-in: $e");
} else {
showRefinedErrorMessage(e);
}
}
}