2022-09-05 21:08:59 +00:00
|
|
|
import { useState } from "react";
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2023-01-05 17:00:16 +00:00
|
|
|
import { getLayout } from "@calcom/features/settings/layouts/SettingsLayout";
|
2022-08-30 19:46:52 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-04-02 10:31:40 +00:00
|
|
|
import { Badge, Meta, Switch, SkeletonButton, SkeletonContainer, SkeletonText, Alert } from "@calcom/ui";
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2023-04-18 18:45:32 +00:00
|
|
|
import PageWrapper from "@components/PageWrapper";
|
2022-11-01 13:29:01 +00:00
|
|
|
import DisableTwoFactorModal from "@components/settings/DisableTwoFactorModal";
|
|
|
|
import EnableTwoFactorModal from "@components/settings/EnableTwoFactorModal";
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2022-12-20 15:13:30 +00:00
|
|
|
const SkeletonLoader = () => {
|
2022-12-07 20:53:44 +00:00
|
|
|
return (
|
|
|
|
<SkeletonContainer>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="mt-6 mb-8 space-y-6">
|
2022-12-07 20:53:44 +00:00
|
|
|
<div className="flex items-center">
|
|
|
|
<SkeletonButton className="mr-6 h-8 w-20 rounded-md p-5" />
|
|
|
|
<SkeletonText className="h-8 w-full" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</SkeletonContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-08-30 19:46:52 +00:00
|
|
|
const TwoFactorAuthView = () => {
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
|
|
|
const { t } = useLocale();
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: user, isLoading } = trpc.viewer.me.useQuery();
|
2022-08-30 19:46:52 +00:00
|
|
|
|
|
|
|
const [enableModalOpen, setEnableModalOpen] = useState(false);
|
|
|
|
const [disableModalOpen, setDisableModalOpen] = useState(false);
|
|
|
|
|
2022-12-20 15:13:30 +00:00
|
|
|
if (isLoading) return <SkeletonLoader />;
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2023-04-02 10:31:40 +00:00
|
|
|
const isCalProvider = user?.identityProvider === "CAL";
|
2022-08-30 19:46:52 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-12-07 20:53:44 +00:00
|
|
|
<Meta title={t("2fa")} description={t("2fa_description")} />
|
2023-04-02 10:31:40 +00:00
|
|
|
{!isCalProvider && <Alert severity="neutral" message={t("2fa_disabled")} />}
|
2022-08-30 19:46:52 +00:00
|
|
|
<div className="mt-6 flex items-start space-x-4">
|
|
|
|
<Switch
|
2023-05-30 14:23:49 +00:00
|
|
|
data-testid="two-factor-switch"
|
2023-04-02 10:31:40 +00:00
|
|
|
disabled={!isCalProvider}
|
2022-08-30 19:46:52 +00:00
|
|
|
checked={user?.twoFactorEnabled}
|
|
|
|
onCheckedChange={() =>
|
|
|
|
user?.twoFactorEnabled ? setDisableModalOpen(true) : setEnableModalOpen(true)
|
|
|
|
}
|
|
|
|
/>
|
2023-04-27 08:07:32 +00:00
|
|
|
<div className="!mx-4">
|
2022-08-30 19:46:52 +00:00
|
|
|
<div className="flex">
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-default font-semibold">{t("two_factor_auth")}</p>
|
2023-04-27 08:07:32 +00:00
|
|
|
<Badge className="mx-2 text-xs" variant={user?.twoFactorEnabled ? "success" : "gray"}>
|
2022-08-30 19:46:52 +00:00
|
|
|
{user?.twoFactorEnabled ? t("enabled") : t("disabled")}
|
|
|
|
</Badge>
|
|
|
|
</div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-default text-sm">{t("add_an_extra_layer_of_security")}</p>
|
2022-08-30 19:46:52 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<EnableTwoFactorModal
|
|
|
|
open={enableModalOpen}
|
|
|
|
onOpenChange={() => setEnableModalOpen(!enableModalOpen)}
|
|
|
|
onEnable={() => {
|
|
|
|
setEnableModalOpen(false);
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.me.invalidate();
|
2022-08-30 19:46:52 +00:00
|
|
|
}}
|
|
|
|
onCancel={() => {
|
|
|
|
setEnableModalOpen(false);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<DisableTwoFactorModal
|
|
|
|
open={disableModalOpen}
|
|
|
|
onOpenChange={() => setDisableModalOpen(!disableModalOpen)}
|
|
|
|
onDisable={() => {
|
|
|
|
setDisableModalOpen(false);
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.me.invalidate();
|
2022-08-30 19:46:52 +00:00
|
|
|
}}
|
|
|
|
onCancel={() => {
|
|
|
|
setDisableModalOpen(false);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
TwoFactorAuthView.getLayout = getLayout;
|
2023-04-18 18:45:32 +00:00
|
|
|
TwoFactorAuthView.PageWrapper = PageWrapper;
|
2022-08-30 19:46:52 +00:00
|
|
|
|
|
|
|
export default TwoFactorAuthView;
|