Add password on confirmation when changing email (#4503)

* Add email confirmation

* Confirm password only if email was changed

Co-authored-by: Omar López <[email protected]>
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 [deleteAccountOpen, setDeleteAccountOpen] = useState(false);
const [hasDeleteErrors, setHasDeleteErrors] = useState(false); const [hasDeleteErrors, setHasDeleteErrors] = useState(false);
const [deleteErrorMessage, setDeleteErrorMessage] = useState(""); 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>) => { const onDeleteMeErrorMutation = (error: TRPCClientErrorLike<AppRouter>) => {
setHasDeleteErrors(true); setHasDeleteErrors(true);
setDeleteErrorMessage(errorMessages[error.message]); 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>) => { const onConfirmButton = (e: Event | React.MouseEvent<HTMLElement, MouseEvent>) => {
e.preventDefault(); e.preventDefault();
const totpCode = form.getValues("totpCode"); const totpCode = form.getValues("totpCode");
@@ -135,7 +153,11 @@ const ProfileView = () => {
<Form <Form
form={formMethods} form={formMethods}
handleSubmit={(values) => { 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" /> <Meta title="Profile" description="Manage settings for your cal profile" />
<div className="flex items-center"> <div className="flex items-center">
@@ -231,6 +253,32 @@ const ProfileView = () => {
</DialogContent> </DialogContent>
</Dialog> </Dialog>
</Form> </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", "no_calendar_installed_description": "You have not yet connected any of your calendars",
"add_a_calendar": "Add a calendar", "add_a_calendar": "Add a calendar",
"change_email_hint": "You may need to log out and back in to see any change take effect", "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" "seats": "seats"
} }
+69 -44
View File
@@ -8,56 +8,81 @@ import { TRPCError } from "@trpc/server";
import { createProtectedRouter } from "../../createRouter"; import { createProtectedRouter } from "../../createRouter";
export const authRouter = createProtectedRouter().mutation("changePassword", { export const authRouter = createProtectedRouter()
input: z.object({ .mutation("changePassword", {
oldPassword: z.string(), input: z.object({
newPassword: z.string(), oldPassword: z.string(),
}), newPassword: z.string(),
async resolve({ input, ctx }) { }),
const { oldPassword, newPassword } = input; async resolve({ input, ctx }) {
const { oldPassword, newPassword } = input;
const { user } = ctx; const { user } = ctx;
if (user.identityProvider !== IdentityProvider.CAL) { if (user.identityProvider !== IdentityProvider.CAL) {
throw new TRPCError({ code: "FORBIDDEN", message: "THIRD_PARTY_IDENTITY_PROVIDER_ENABLED" }); throw new TRPCError({ code: "FORBIDDEN", message: "THIRD_PARTY_IDENTITY_PROVIDER_ENABLED" });
} }
const currentPasswordQuery = await prisma.user.findFirst({ const currentPasswordQuery = await prisma.user.findFirst({
where: { where: {
id: user.id, id: user.id,
}, },
select: { select: {
password: true, password: true,
}, },
}); });
const currentPassword = currentPasswordQuery?.password; const currentPassword = currentPasswordQuery?.password;
if (!currentPassword) { if (!currentPassword) {
throw new TRPCError({ code: "NOT_FOUND", message: "MISSING_PASSWORD" }); throw new TRPCError({ code: "NOT_FOUND", message: "MISSING_PASSWORD" });
} }
const passwordsMatch = await verifyPassword(oldPassword, currentPassword); const passwordsMatch = await verifyPassword(oldPassword, currentPassword);
if (!passwordsMatch) { if (!passwordsMatch) {
throw new TRPCError({ code: "BAD_REQUEST", message: "INCORRECT_PASSWORD" }); throw new TRPCError({ code: "BAD_REQUEST", message: "INCORRECT_PASSWORD" });
} }
if (oldPassword === newPassword) { if (oldPassword === newPassword) {
throw new TRPCError({ code: "BAD_REQUEST", message: "PASSWORD_MATCHES_OLD" }); throw new TRPCError({ code: "BAD_REQUEST", message: "PASSWORD_MATCHES_OLD" });
} }
if (!validPassword(newPassword)) { if (!validPassword(newPassword)) {
throw new TRPCError({ code: "BAD_REQUEST", message: "INVALID_PASSWORD" }); throw new TRPCError({ code: "BAD_REQUEST", message: "INVALID_PASSWORD" });
} }
const hashedPassword = await hashPassword(newPassword); const hashedPassword = await hashPassword(newPassword);
await prisma.user.update({ await prisma.user.update({
where: { where: {
id: user.id, id: user.id,
}, },
data: { data: {
password: hashedPassword, password: hashedPassword,
}, },
}); });
}, },
}); })
.mutation("verifyPassword", {
input: z.object({
passwordInput: z.string(),
}),
async resolve({ input, ctx }) {
const user = await prisma.user.findUnique({
where: {
id: ctx.user.id,
},
});
if (!user?.password) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
}
const passwordsMatch = await verifyPassword(input.passwordInput, user.password);
if (!passwordsMatch) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return;
},
});
+1 -1
View File
@@ -269,7 +269,7 @@ export const PasswordField = forwardRef<HTMLInputElement, InputFieldProps>(funct
<Tooltip content={textLabel}> <Tooltip content={textLabel}>
<button <button
className="absolute bottom-0 right-3 h-9 text-gray-900" className="absolute bottom-2 right-3 h-9 text-gray-900"
type="button" type="button"
onClick={() => toggleIsPasswordVisible()}> onClick={() => toggleIsPasswordVisible()}>
{isPasswordVisible ? ( {isPasswordVisible ? (