Files
calendar/apps/web/components/security/DisableTwoFactorModal.tsx
T
Agusti Fernandez PardoGitHubAgusti Fernandez PardoOmar Lópezkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
f4fe91396f Improve 2fa: ask for code before account removal and 2fa disabling (#3817)
* fix conflicts

* fix remove separate function and call mutation directly

* feat: add new react-otp-input to enable 2fa flow

* fix: comment out

* fix: remove next-auth 4.9.0 from yarn.lock

* fix: delete account test fill password before submit

* fix: test delete accc

* fix typo in delete acc test

* Update apps/web/components/security/EnableTwoFactorModal.tsx

Co-authored-by: Omar López <zomars@me.com>

* feat: remove react-otp-input reuse TwoFactor

* feat: add center props to TwoFactor

* fix: no v2

* feat: disable 2fa requires 2fa api

* feat: make 2fa required to disable 2fa

* fix: FormEvent instead of SyntheticEvent

* fix: types

* fix: move disable 2fa form to fully use RHF

* fix     if (e) e.preventDefault();

* feat: fix remove account

* fix: remove react-otp-input types

* fix: separate onConfirm to add to form handleSubmit

* fix: types e:SyntethicEvent

* fix: types

* fix: import packages lib not web lib

* Update apps/web/components/security/EnableTwoFactorModal.tsx

Co-authored-by: Omar López <zomars@me.com>

* Update apps/web/components/security/EnableTwoFactorModal.tsx

Co-authored-by: Omar López <zomars@me.com>

* fix: no import from web

* fix: import

* fix: remove duplicate FormEvent

* fix: upgrade ErrorCode imports

* fix profile types totpCode not optional

* fix: build pass

* fix: dont touch test delete-account

* fix: type

* fix: add data-testid to password field

* fix: conflicts w syncServices

* Build fixes

* Fixes delete account e2e test

Co-authored-by: Agusti Fernandez Pardo <git@agusti.me>
Co-authored-by: Omar López <zomars@me.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-08-31 14:57:53 -06:00

102 lines
3.3 KiB
TypeScript

import { useState } from "react";
import { useForm } from "react-hook-form";
import { ErrorCode } from "@calcom/lib/auth";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import Button from "@calcom/ui/Button";
import { Dialog, DialogContent } from "@calcom/ui/Dialog";
import { Form, Label } from "@calcom/ui/form/fields";
import { PasswordField } from "@calcom/ui/v2/core/form/fields";
import TwoFactor from "@components/auth/TwoFactor";
import TwoFactorAuthAPI from "./TwoFactorAuthAPI";
import TwoFactorModalHeader from "./TwoFactorModalHeader";
interface DisableTwoFactorAuthModalProps {
/** Called when the user closes the modal without disabling two-factor auth */
onCancel: () => void;
/** Called when the user disables two-factor auth */
onDisable: () => void;
}
interface DisableTwoFactorValues {
totpCode: string;
password: string;
}
const DisableTwoFactorAuthModal = ({ onDisable, onCancel }: DisableTwoFactorAuthModalProps) => {
const [isDisabling, setIsDisabling] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const { t } = useLocale();
const form = useForm<DisableTwoFactorValues>();
async function handleDisable({ totpCode, password }: DisableTwoFactorValues) {
if (isDisabling) {
return;
}
setIsDisabling(true);
setErrorMessage(null);
try {
const response = await TwoFactorAuthAPI.disable(password, totpCode);
if (response.status === 200) {
onDisable();
return;
}
const body = await response.json();
if (body.error === ErrorCode.IncorrectPassword) {
setErrorMessage(t("incorrect_password"));
}
if (body.error === ErrorCode.SecondFactorRequired) {
setErrorMessage(t("2fa_required"));
}
if (body.error === ErrorCode.IncorrectTwoFactorCode) {
setErrorMessage(t("incorrect_2fa"));
} else {
setErrorMessage(t("something_went_wrong"));
}
} catch (e) {
setErrorMessage(t("something_went_wrong"));
console.error(t("error_disabling_2fa"), e);
} finally {
setIsDisabling(false);
}
}
return (
<Dialog open={true}>
<DialogContent>
<Form form={form} handleSubmit={handleDisable}>
<TwoFactorModalHeader title={t("disable_2fa")} description={t("disable_2fa_recommendation")} />
<div className="mb-4">
<PasswordField
labelProps={{
className: "block text-sm font-medium text-gray-700",
}}
{...form.register("password")}
className="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:border-black focus:outline-none focus:ring-black"
/>
<Label className="mt-4"> {t("2fa_code")}</Label>
<TwoFactor center={false} />
{errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>}
</div>
<div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
<Button type="submit" className="ltr:ml-2 rtl:mr-2" disabled={isDisabling}>
{t("disable")}
</Button>
<Button color="secondary" onClick={onCancel}>
{t("cancel")}
</Button>
</div>
</Form>
</DialogContent>
</Dialog>
);
};
export default DisableTwoFactorAuthModal;