Skip to content

The open alternative to Salesforce, designed for AI.

4.6 KB
Raw Blame
import { useAuth } from '@/auth/hooks/useAuth';
import { currentUserState } from '@/auth/states/currentUserState';
import { useCurrentUserWorkspaceTwoFactorAuthentication } from '@/settings/two-factor-authentication/hooks/useCurrentUserWorkspaceTwoFactorAuthentication';
import { useCurrentWorkspaceTwoFactorAuthenticationPolicy } from '@/settings/two-factor-authentication/hooks/useWorkspaceTwoFactorAuthenticationPolicy';
import { ConfirmationDialog } from '@/ui/layout/dialog/components/ConfirmationDialog';
import { useDialog } from '@/ui/layout/dialog/hooks/useDialog';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useLoadCurrentUser } from '@/users/hooks/useLoadCurrentUser';
import { useMutation } from '@apollo/client/react';
import { Trans, useLingui } from '@lingui/react/macro';
import { useParams } from 'react-router-dom';
import { SettingsPath } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { Section } from 'twenty-ui/components/layout';
import { Button } from 'twenty-ui/primitives/input';
import { DeleteTwoFactorAuthenticationMethodDocument } from '~/generated-metadata/graphql';
import { useNavigateSettings } from '~/hooks/useNavigateSettings';

const DELETE_TWO_FACTOR_AUTHENTICATION_MODAL_ID =
  'delete-two-factor-authentication-modal';
export const DeleteTwoFactorAuthentication = () => {
  const { t } = useLingui();
  const { openDialog } = useDialog();

  const { enqueueToast } = useToast();
  const { signOut } = useAuth();
  const { loadCurrentUser } = useLoadCurrentUser();
  const [deleteTwoFactorAuthenticationMethod] = useMutation(
    DeleteTwoFactorAuthenticationMethodDocument,
  );
  const currentUser = useAtomStateValue(currentUserState);
  const userEmail = currentUser?.email;
  const navigate = useNavigateSettings();
  const twoFactorAuthenticationStrategy =
    useParams().twoFactorAuthenticationStrategy;

  const { currentUserWorkspaceTwoFactorAuthenticationMethods } =
    useCurrentUserWorkspaceTwoFactorAuthentication();

  const { isEnforced: isTwoFactorAuthenticationEnforced } =
    useCurrentWorkspaceTwoFactorAuthenticationPolicy();

  const reset2FA = async () => {
    if (
      !isDefined(twoFactorAuthenticationStrategy) ||
      !isDefined(
        currentUserWorkspaceTwoFactorAuthenticationMethods[
          twoFactorAuthenticationStrategy
        ]?.twoFactorAuthenticationMethodId,
      )
    ) {
      enqueueToast({
        status: 'error',
        children: t`Invalid 2FA information.`,
        dedupeKey: '2fa-dedupe-key',
      });
      return navigate(SettingsPath.ProfilePage);
    }

    await deleteTwoFactorAuthenticationMethod({
      variables: {
        twoFactorAuthenticationMethodId:
          currentUserWorkspaceTwoFactorAuthenticationMethods[
            twoFactorAuthenticationStrategy
          ].twoFactorAuthenticationMethodId,
      },
    });

    enqueueToast({
      status: 'success',
      children: t`2FA Method has been deleted successfully.`,
      dedupeKey: '2fa-dedupe-key',
    });

    if (isTwoFactorAuthenticationEnforced === true) {
      await signOut();
    } else {
      navigate(SettingsPath.ProfilePage);
      await loadCurrentUser();
    }
  };

  return (
    <>
      <Section.Header
        title={t`Delete Two-Factor Authentication Method`}
        description={t`Deleting this method will remove it permanently from your account.`}
      />

      <Button
        onClick={() => openDialog(DELETE_TWO_FACTOR_AUTHENTICATION_MODAL_ID)}
        variant="outline"
        color="danger"
      >{t`Reset 2FA`}</Button>

      <ConfirmationDialog
        confirmationValue={userEmail}
        confirmationPlaceholder={userEmail ?? ''}
        dialogId={DELETE_TWO_FACTOR_AUTHENTICATION_MODAL_ID}
        title={t`2FA Method Reset`}
        subtitle={
          isTwoFactorAuthenticationEnforced ? (
            <Trans>
              This will permanently delete your two factor authentication
              method.
              <br />
              Since 2FA is mandatory in your workspace, you will be logged out
              after deletion and will be asked to configure it again upon login.{' '}
              <br />
              Please type in your email to confirm.
            </Trans>
          ) : (
            <Trans>
              This action cannot be undone. This will permanently reset your two
              factor authentication method. <br /> Please type in your email to
              confirm.
            </Trans>
          )
        }
        onConfirmClick={reset2FA}
        confirmButtonText={t`Reset 2FA`}
      />
    </>
  );
};