signUp method
Implementation
Future<void> signUp() async {
print('subscriptionType-AAA: ${subscriptionType.value}');
try {
if (!validateSignupFields()) {
return;
}
bool userExists = await checkUserExistence(
emailTxtController.text,
phoneNumberController.text.trim(),
);
if (userExists) return;
var result = await checkReferralCode(referralCodeTxtController.text);
if (!result) {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid Invitation Code",
duration: Duration(seconds: 10),
message:
"The invitation code you entered is invalid. Please try again.",
actions: [
FlashAction(
label: 'Contact Support',
onPressed: () {
launchEmail();
},
),
FlashAction(
label: 'Try Again',
),
],
);
return;
}
loadingDialog();
try {
isEmailAuth.value = true;
PhoneAuthCredential? phoneAuthCredential = await verifyPhoneNumber();
print("PhoneAuthCredential${phoneAuthCredential.toString()}");
if (phoneAuthCredential == null) {
closeDialog();
return;
}
UserCredential phoneVerificationResult;
try {
phoneVerificationResult = await FirebaseAuth.instance
.signInWithCredential(phoneAuthCredential);
if (phoneVerificationResult.user == null) {
closeDialog();
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid OTP",
message: "The OTP you entered is not valid. Please enter the correct one",
);
return;
}
} catch (e) {
closeDialog();
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid OTP",
message: "The OTP you entered is not valid. Please enter the correct one",
);
return;
}
AuthCredential emailAuthCredential = EmailAuthProvider.credential(
email: emailTxtController.text.trim(),
password: passTxtController.text.trim(),
);
User? user = phoneVerificationResult.user;
if (user == null) {
closeDialog();
return;
}
try {
await user.linkWithCredential(emailAuthCredential);
await user.updateDisplayName(
'${firstNameTxtController.text} ${lastNameTxtController.text}');
print("countryCode: ${countryCodeController.text}");
print("phoneNumber: ${phoneNumberController.text}");
}
catch(e){
print(e.toString());
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Unable to Register",
message: "We were unable to Register you at the moment. Please try again Later",
);
await user.delete();
return ;
}
final loginResult = await Requests.createUser(
user.uid,
firstName: firstNameTxtController.text,
email: emailTxtController.text,
lastName: lastNameTxtController.text,
phoneNo: phoneNumberController.text.isEmpty
? mobileNumTxtController.text
: phoneNumberController.text,
countryCode: countryCodeController.text,
referralCode: referralCodeTxtController.text,
type:
subscriptionType.value == "Business" ? "CORPORATE" : "INDIVIDUAL",
);
if (loginResult?['statusCode'] == 200) {
await linkEmailPassword(user, loginResult?['referrerCode']);
await linkEmailPassword(user, loginResult?['referrerCode']);
} else {
closeDialog();
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message:
"Unable to sign up. Please make sure you are not already signed up!",
);
userExists = await checkUserExistence(
emailTxtController.text,
phoneNumberController.text.trim(),
);
if(!userExists){
await user.delete();
}
}
} on FirebaseAuthException catch (e) {
closeDialog();
if (e.code == 'email-already-in-use') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message:
"The account with this email address already exsist, please continue with login.",
);
signinSignupTab.value == 'SIGN-IN';
} else if (e.code == 'account-exists-with-different-credential') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message:
"An account already exists with the same email address but different sign-in credentials. Try signing in with Google, Facebook, or another method you used before.",
);
} else if (e.code == 'too-many-requests') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Too Many Requests",
message:
"Too many attempts have been made. To protect your account, requests from this device have been temporarily blocked. Please try again later.",
);
} else if (e.code == 'invalid-email') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid Email",
message:
"The email address format is invalid. Please check and try again.",
);
} else if (e.code == 'weak-password') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Weak Password",
message:
"Your password is too weak. Please use a stronger password.",
);
} else if (e.code == 'operation-not-allowed') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Operation Not Allowed",
message:
"This sign-in method is currently disabled. Please contact support.",
);
} else {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Authentication Error",
message: e.message ?? "Something went wrong. Please try again.",
);
}
print('Error: ${e.message}');
} catch (e) {
closeDialog();
showRefinedErrorMessage(e);
}
} catch (e) {
closeDialog();
showRefinedErrorMessage(e);
}
}