Add password on confirmation when changing email (#4503)

* Add email confirmation

* Confirm password only if email was changed

Co-authored-by: Omar López <zomars@me.com>
This commit is contained in:
Joe Au-YeungandOmar López authored and GitHub committed 2022-09-17 23:27:25 +01:00
1 parent 2ef8e230c5
commit cf32287109
4 files changed
+120 -46

No files matched your search

@@ -59,6 +59,8 @@ const ProfileView = () => {
},
});
const [confirmPasswordOpen, setConfirmPasswordOpen] = useState(false);
const [confirmPasswordErrorMessage, setConfirmPasswordDeleteErrorMessage] = useState("");
const [deleteAccountOpen, setDeleteAccountOpen] = useState(false);
const [hasDeleteErrors, setHasDeleteErrors] = useState(false);
const [deleteErrorMessage, setDeleteErrorMessage] = useState("");
@@ -82,6 +84,16 @@ const ProfileView = () => {
}
};
const confirmPasswordMutation = trpc.useMutation("viewer.auth.verifyPassword", {
onSuccess() {
mutation.mutate(formMethods.getValues());
setConfirmPasswordOpen(false);
},
onError() {
setConfirmPasswordDeleteErrorMessage(t("incorrect_password"));
},
});
const onDeleteMeErrorMutation = (error: TRPCClientErrorLike<AppRouter>) => {
setHasDeleteErrors(true);
setDeleteErrorMessage(errorMessages[error.message]);
@@ -94,6 +106,12 @@ const ProfileView = () => {
},
});
const onConfirmPassword = (e: Event | React.MouseEvent<HTMLElement, MouseEvent>) => {
e.preventDefault();
const password = passwordRef.current.value;
confirmPasswordMutation.mutate({ passwordInput: password });
};
const onConfirmButton = (e: Event | React.MouseEvent<HTMLElement, MouseEvent>) => {
e.preventDefault();
const totpCode = form.getValues("totpCode");
@@ -135,7 +153,11 @@ const ProfileView = () => {
<Form
form={formMethods}
handleSubmit={(values) => {
mutation.mutate(values);
if (values.email !== user?.email) {
setConfirmPasswordOpen(true);
} else {
mutation.mutate(values);
}
}}>
<Meta title="Profile" description="Manage settings for your cal profile" />
<div className="flex items-center">
@@ -231,6 +253,32 @@ const ProfileView = () => {
</DialogContent>
</Dialog>
</Form>
{/* If changing email, confirm password */}
<Dialog open={confirmPasswordOpen} onOpenChange={setConfirmPasswordOpen}>
<DialogContent
title={t("confirm_password")}
description={t("confirm_password_change_email")}
type="creation"
actionText={t("confirm")}
Icon={Icon.FiAlertTriangle}
actionOnClick={(e) => e && onConfirmPassword(e)}>
<>
<PasswordField
data-testid="password"
name="password"
id="password"
type="password"
autoComplete="current-password"
required
label="Password"
ref={passwordRef}
/>
{confirmPasswordErrorMessage && <Alert severity="error" title={confirmPasswordErrorMessage} />}
</>
</DialogContent>
</Dialog>
</>
);
};
@@ -1266,5 +1266,6 @@
"no_calendar_installed_description": "You have not yet connected any of your calendars",
"add_a_calendar": "Add a calendar",
"change_email_hint": "You may need to log out and back in to see any change take effect",
"confirm_password_change_email": "Please confirm your password before changing your email address",
"seats": "seats"
}