The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { useAuth } from '@/auth/hooks/useAuth';
import {
StyledTwoFactorInstructions,
StyledTwoFactorMainContent,
} from '@/auth/sign-in-up/components/internal/SignInUpTwoFactorAuthenticationStyles';
import {
type OTPFormValues,
useTwoFactorAuthenticationForm,
} from '@/auth/sign-in-up/hooks/useTwoFactorAuthenticationForm';
import { loginTokenState } from '@/auth/states/loginTokenState';
import {
SignInUpStep,
signInUpStepState,
} from '@/auth/states/signInUpStepState';
import { getTwoFactorAuthenticationErrorToastOptions } from '@/auth/utils/getTwoFactorAuthenticationErrorToastOptions';
import { useReadCaptchaToken } from '@/captcha/hooks/useReadCaptchaToken';
import { useCaptcha } from '@/client-config/hooks/useCaptcha';
import { ONBOARDING_CONTENT_BLOCK_WIDTH } from '@/onboarding/constants/OnboardingContentBlockWidth';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
import { styled } from '@linaria/react';
import { Trans, useLingui } from '@lingui/react/macro';
import { OTPInput, type SlotProps } from 'input-otp';
import { useState } from 'react';
import { Controller } from 'react-hook-form';
import { AppPath } from 'twenty-shared/types';
import { useToast } from 'twenty-ui/components/feedback';
import { MainButton } from 'twenty-ui/components/input';
import { Button } from 'twenty-ui/primitives/input';
import { themeCssVariables } from 'twenty-ui/theme';
import { useNavigateApp } from '~/hooks/useNavigateApp';
const StyledForm = styled.form`
align-items: center;
display: flex;
flex-direction: column;
max-width: 100%;
width: ${ONBOARDING_CONTENT_BLOCK_WIDTH}px;
`;
const StyledSlot = styled.div<{ isActive: boolean }>`
align-items: center;
border-bottom: 1px solid ${themeCssVariables.border.color.medium};
border-right: 1px solid ${themeCssVariables.border.color.medium};
border-top: 1px solid ${themeCssVariables.border.color.medium};
display: flex;
font-size: 2rem;
height: 3.5rem;
justify-content: center;
outline-color: ${({ isActive }) =>
isActive
? themeCssVariables.border.color.strong
: themeCssVariables.border.color.medium};
outline-style: ${({ isActive }) => (isActive ? 'solid' : 'none')};
outline-width: ${({ isActive }) => (isActive ? '1px' : '0')};
&:first-of-type {
border-bottom-left-radius: 0.375rem;
border-left: 1px solid ${themeCssVariables.border.color.medium};
border-top-left-radius: 0.375rem;
}
&:last-of-type {
border-bottom-right-radius: 0.375rem;
border-top-right-radius: 0.375rem;
}
.group:hover &,
.group:focus-within & {
border-color: ${themeCssVariables.border.color.medium};
}
position: relative;
transition: all 0.3s;
width: 2.5rem;
`;
const StyledPlaceholderChar = styled.div`
.group:has(input[data-input-otp-placeholder-shown]) & {
opacity: 0.2;
}
`;
export const Slot = (props: SlotProps) => {
return (
<StyledSlot isActive={props.isActive}>
<StyledPlaceholderChar>
{props.char ?? props.placeholderChar}
</StyledPlaceholderChar>
{props.hasFakeCaret && <FakeCaret />}
</StyledSlot>
);
};
const StyledCaretContainer = styled.div`
align-items: center;
animation: caret-blink 1s steps(2, start) infinite;
display: flex;
inset: 0;
justify-content: center;
pointer-events: none;
position: absolute;
@keyframes caret-blink {
0%,
100% {
opacity: 1;
}
50% {
opacity: 0;
}
}
`;
const StyledCaret = styled.div`
background-color: ${themeCssVariables.font.color.light};
height: 2rem;
width: 1px;
`;
const FakeCaret = () => {
return (
<StyledCaretContainer>
<StyledCaret />
</StyledCaretContainer>
);
};
const StyledDashContainer = styled.div`
align-items: center;
display: flex;
justify-content: center;
width: 2.5rem;
`;
const StyledDash = styled.div`
background-color: ${themeCssVariables.font.color.primary};
border-radius: ${themeCssVariables.border.radius.pill};
corner-shape: round;
height: 0.25rem;
width: 0.75rem;
`;
const FakeDash = () => {
return (
<StyledDashContainer>
<StyledDash />
</StyledDashContainer>
);
};
const StyledOTPContainer = styled.div`
align-items: center;
display: flex;
&:has(:disabled) {
opacity: 0.3;
}
`;
const StyledSlotGroup = styled.div`
display: flex;
`;
const StyledActionBackLinkContainer = styled.div`
margin: ${themeCssVariables.spacing[3]} 0 0;
`;
export const SignInUpTOTPVerification = () => {
const [isLoading, setIsLoading] = useState<boolean>(false);
const { getAuthTokensFromOTP } = useAuth();
const { enqueueToast } = useToast();
const navigate = useNavigateApp();
const { readCaptchaToken } = useReadCaptchaToken();
const { isCaptchaReady } = useCaptcha();
const loginToken = useAtomStateValue(loginTokenState);
const setSignInUpStep = useSetAtomState(signInUpStepState);
const { t } = useLingui();
const { form } = useTwoFactorAuthenticationForm();
const submitOTP = async (values: OTPFormValues) => {
setIsLoading(true);
try {
if (!isCaptchaReady) {
enqueueToast({
status: 'error',
children: t`Captcha (anti-bot check) is still loading, try again`,
});
setIsLoading(false);
return;
}
const captchaToken = readCaptchaToken();
if (!loginToken) {
return navigate(AppPath.SignInUp);
}
await getAuthTokensFromOTP(values.otp, loginToken, captchaToken);
} catch (error) {
form.setValue('otp', '');
enqueueToast(
getTwoFactorAuthenticationErrorToastOptions({
error,
dedupeKey: 'invalid-otp-dedupe-key',
}),
);
} finally {
setIsLoading(false);
}
};
const handleBack = () => {
setSignInUpStep(SignInUpStep.TwoFactorAuthenticationProvision);
};
const handleUseRecoveryCode = () => {
setSignInUpStep(SignInUpStep.TwoFactorAuthenticationRecovery);
};
return (
<StyledForm onSubmit={form.handleSubmit(submitOTP)}>
<StyledTwoFactorInstructions>
<Trans>Paste the code below</Trans>
</StyledTwoFactorInstructions>
<StyledTwoFactorMainContent>
{/* // oxlint-disable-next-line react/jsx-props-no-spreading */}
<Controller
name="otp"
control={form.control}
render={({ field: { onChange, onBlur, value } }) => (
<OTPInput
maxLength={6}
onBlur={onBlur}
onChange={onChange}
value={value}
autoFocus
render={({ slots }) => (
<StyledOTPContainer>
<StyledSlotGroup>
{slots.slice(0, 3).map((slot, idx) => (
<Slot
key={idx}
// oxlint-disable-next-line react/jsx-props-no-spreading
{...slot}
/>
))}
</StyledSlotGroup>
<FakeDash />
<StyledSlotGroup>
{slots.slice(3).map((slot, idx) => (
<Slot
key={idx}
// oxlint-disable-next-line react/jsx-props-no-spreading
{...slot}
/>
))}
</StyledSlotGroup>
</StyledOTPContainer>
)}
/>
)}
/>
</StyledTwoFactorMainContent>
<MainButton
type="submit"
fullWidth
disabled={isLoading}
>{t`Submit`}</MainButton>
<StyledActionBackLinkContainer>
<Button variant="link" onClick={handleUseRecoveryCode}>
<Trans>Lost your authenticator? Use a recovery code</Trans>
</Button>
</StyledActionBackLinkContainer>
<StyledActionBackLinkContainer>
<Button variant="link" onClick={handleBack}>
<Trans>Back</Trans>
</Button>
</StyledActionBackLinkContainer>
</StyledForm>
);
};