changePassword method
Implementation
void changePassword() async {
if (currentPasswordController.text.isEmpty) {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message: "Please enter current password field",
);
// DefaultSnackbar.show("Error", "Please enter current password field");
return;
}
if (newPasswordController.text.length < 8) {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message: "Password must be at least 8 characters long",
);
// DefaultSnackbar.show(
// "Error", "Password must be at least 8 characters long");
return;
}
if (confirmPasswordController.text != newPasswordController.text) {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message: "New Password and confirmation do not match",
);
// DefaultSnackbar.show("Error", "New Password and confirmation do not match");
return;
}
if (currentPasswordController.text == newPasswordController.text) {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid new password",
message: "New password should be different from old password.",
);
// DefaultSnackbar.show("Invalid new password",
// "New password should be different from old password.");
return;
}
try {
final email =
authenticationController.userProfileModel.value.data?.user?.email;
if (email == null) {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid password",
message: "Current password does not match.",
);
// DefaultSnackbar.show(
// "Invalid password", "Current password does not match.");
return;
}
try {
loadingDialog();
AuthCredential cred = EmailAuthProvider.credential(
email: email,
password: currentPasswordController.text,
);
UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(cred);
closeDialog();
if (userCredential.user == null) {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid password",
message: "Current password does not match.",
);
// 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") {
{
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid password",
message: "Current password does not match.",
);
// DefaultSnackbar.show(
// "Invalid password", "Current password does not match.");
}
return;
} else {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid password",
message: "Current password does not match.",
);
// DefaultSnackbar.show(
// "Invalid password", "Current password does not match.");
return;
}
}
final response = await Requests.getDio().put(
'/users/update-password',
data: {
"password": newPasswordController.text,
},
);
if (response.statusCode == 200) {
CustomFlashWidget.showFlashMessage(
type: FlashType.success,
title: "Success",
message: "Password updated successfully",
);
// DefaultSnackbar.show("Success", "Password updated successfully");
currentPasswordController.text = "";
newPasswordController.text = "";
confirmPasswordController.text = "";
} else {
log('Error changing password : ${response.data}');
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message: "Error occurred",
);
// DefaultSnackbar.show("Error", "Error occurred");
}
} catch (_) {
log('Error changing password : ${_.toString()}');
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message: "Error occurred",
);
// DefaultSnackbar.show("Error", "Error occurred");
}
}