changePassword method
Implementation
changePassword() async {
if (currentPasswordController.text.isEmpty) {
DefaultSnackbar.show(
"Invalid current password", "Please enter current password field");
return;
}
if (newPasswordController.text.length < 8) {
DefaultSnackbar.show("Invalid new password",
"Password must be at least 8 characters long");
return;
}
if (confirmPasswordController.text != newPasswordController.text) {
DefaultSnackbar.show(
"Invalid confirm password", "Password and confirmation do not match");
return;
}
if (currentPasswordController.text == newPasswordController.text) {
DefaultSnackbar.show("Invalid new password",
"New password should be different from old password.");
return;
}
try {
final email = authenticationController.userProfileModel.value.data?.email;
if (email == null) {
DefaultSnackbar.show(
"Invalid password", "Current password does not match.");
return;
}
try {
Get.dialog(
barrierColor: ColorHelper.loaderBarrierColor,
const LoadingDialog());
AuthCredential cred = EmailAuthProvider.credential(
email: email,
password: currentPasswordController.text,
);
UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(cred);
if (userCredential.user == null) {
DefaultSnackbar.show(
"Invalid password", "Current password does not match.");
return;
}
} on FirebaseAuthException catch (e) {
closeDialog();
if (e.code == "wrong-password" ||
e.code == "invalid-login-credentials") {
{
DefaultSnackbar.show(
"Invalid password", "Current password does not match.");
}
return;
} else {
DefaultSnackbar.show(
"Invalid password", "Current password does not match.");
return;
}
}
final response = await Requests.getDio(
showLoadingDialog: false,
).put(
'/api/users/update-password',
data: {
"password": newPasswordController.text,
},
);
if (response.statusCode == 200) {
closeDialog();
DefaultSnackbar.show("Success", "Password updated successfully");
currentPasswordController.text = "";
newPasswordController.text = "";
confirmPasswordController.text = "";
isEditingPassword.value = false;
} else {
log('Error changing password : ${response.data}');
closeDialog();
final String? errorMsg = response.data?['message'];
DefaultSnackbar.show("Error", errorMsg ?? "Error occurred");
}
} catch (_) {
log('Error changing password : ${_.toString()}');
closeDialog();
DefaultSnackbar.show("Error", "Error occurred");
}
}