The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { Button } from 'twenty-ui/primitives/input';
import { styled } from '@linaria/react';
import { Trans, useLingui } from '@lingui/react/macro';
import { isNonEmptyString } from '@sniptt/guards';
import { FormProvider } from 'react-hook-form';
import QRCodeModule from 'react-qr-code';
import { qrCodeState } from '@/auth/states/qrCode';
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { DeleteTwoFactorAuthentication } from '@/settings/two-factor-authentication/components/DeleteTwoFactorAuthenticationMethod';
import { TwoFactorAuthenticationSetupForSettingsEffect } from '@/settings/two-factor-authentication/components/TwoFactorAuthenticationSetupForSettingsEffect';
import { TwoFactorAuthenticationVerificationForSettings } from '@/settings/two-factor-authentication/components/TwoFactorAuthenticationVerificationForSettings';
import { useCurrentUserWorkspaceTwoFactorAuthentication } from '@/settings/two-factor-authentication/hooks/useCurrentUserWorkspaceTwoFactorAuthentication';
import { useTwoFactorVerificationForSettings } from '@/settings/two-factor-authentication/hooks/useTwoFactorVerificationForSettings';
import { extractSecretFromOtpUri } from '@/settings/two-factor-authentication/utils/extractSecretFromOtpUri';
import { SettingsPageLayout } from '@/settings/components/layout/SettingsPageLayout';
import { SettingsPath } from 'twenty-shared/types';
import { getSettingsPath, isDefined } from 'twenty-shared/utils';
import { Section } from 'twenty-ui/components/layout';
import { Loader } from 'twenty-ui/primitives/feedback';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { themeCssVariables } from 'twenty-ui/theme';
import { resolveCjsModuleDefaultExport } from '~/utils/resolveCjsModuleDefaultExport';
import { useCopyToClipboard } from '~/hooks/useCopyToClipboard';
const QRCode = resolveCjsModuleDefaultExport(QRCodeModule);
const StyledQRCodeContainer = styled.div`
align-items: flex-start;
display: flex;
flex-direction: column;
gap: ${themeCssVariables.spacing[3]};
margin: ${themeCssVariables.spacing[4]} 0;
`;
const StyledQRCodeWrapper = styled.div`
align-items: center;
background-color: ${themeCssVariables.background.secondary};
border: 1px solid ${themeCssVariables.border.color.light};
border-radius: ${themeCssVariables.border.radius.md};
display: flex;
justify-content: center;
padding: ${themeCssVariables.spacing[4]};
`;
const StyledOTPContainer = styled.div`
width: fit-content;
`;
const StyledQRCodeSizer = styled.div`
height: 137px;
width: 137px;
& > svg {
height: 100%;
width: 100%;
}
`;
const StyledCopySetupKeyText = styled.div`
color: ${themeCssVariables.font.color.tertiary};
font-size: ${themeCssVariables.font.size.sm};
line-height: 1.5;
text-align: left;
`;
const StyledDivider = styled.div`
margin: ${themeCssVariables.spacing[6]} 0;
width: 100%;
`;
export const SettingsTwoFactorAuthenticationMethod = () => {
const { t } = useLingui();
const { copyToClipboard } = useCopyToClipboard();
const qrCode = useAtomStateValue(qrCodeState);
const { currentUserWorkspaceTwoFactorAuthenticationMethods } =
useCurrentUserWorkspaceTwoFactorAuthentication();
const has2FAMethod =
currentUserWorkspaceTwoFactorAuthenticationMethods['TOTP']?.status ===
'VERIFIED';
const verificationForm = useTwoFactorVerificationForSettings();
const shouldShowActionButtons = !has2FAMethod;
const handleCopySetupKey = async () => {
if (!isNonEmptyString(qrCode)) {
return;
}
const secret = extractSecretFromOtpUri(qrCode);
if (isDefined(secret)) {
await copyToClipboard(secret, t`Setup key copied to clipboard`);
}
};
return (
// oxlint-disable-next-line react/jsx-props-no-spreading
<FormProvider {...verificationForm.formConfig}>
<SettingsPageLayout
title={t`Two Factor Authentication`}
links={[
{
children: <Trans>User</Trans>,
href: getSettingsPath(SettingsPath.ProfilePage),
},
{
children: <Trans>Profile</Trans>,
href: getSettingsPath(SettingsPath.ProfilePage),
},
{
children: <Trans>Two-Factor Authentication</Trans>,
},
]}
actionButton={
shouldShowActionButtons ? (
<SaveAndCancelButtons
isSaveDisabled={!verificationForm.canSave}
isCancelDisabled={verificationForm.isSubmitting}
isLoading={verificationForm.isLoading}
onCancel={verificationForm.handleCancel}
onSave={verificationForm.formConfig.handleSubmit(
verificationForm.handleSave,
)}
/>
) : undefined
}
>
<SettingsPageContainer>
{has2FAMethod ? (
<Section.Root>
<DeleteTwoFactorAuthentication />
</Section.Root>
) : (
<Section.Root>
<TwoFactorAuthenticationSetupForSettingsEffect />
<Section.Header
title={t`Authenticator app`}
description={t`Authenticator apps and browser extensions like 1Password, Authy, Microsoft Authenticator, etc. generate one-time passwords that are used as a second factor to verify your identity when prompted during sign-in.`}
/>
<StyledQRCodeContainer>
{!qrCode ? (
<Loader />
) : (
<>
<StyledQRCodeWrapper>
<StyledQRCodeSizer>
<QRCode value={qrCode} />
</StyledQRCodeSizer>
</StyledQRCodeWrapper>
<StyledCopySetupKeyText>
<Trans>Can't scan? Copy the</Trans>{' '}
<Button
variant="link"
size="sm"
onClick={handleCopySetupKey}
>
<Trans>setup key</Trans>
</Button>
</StyledCopySetupKeyText>
</>
)}
</StyledQRCodeContainer>
<StyledDivider />
<Section.Header
title={t`Verify the code from the app`}
description={t`Copy paste the code below`}
/>
<StyledOTPContainer>
<TwoFactorAuthenticationVerificationForSettings />
</StyledOTPContainer>
</Section.Root>
)}
</SettingsPageContainer>
</SettingsPageLayout>
</FormProvider>
);
};