Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 6x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 10x 1x 10x 2x 10x 1x 1x 1x 10x 48x 6x 8x | import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import { maskEmail } from "@hooks/auth-api/useLoginOTP";
import { Box, BoxProps, Stack } from "@mui/material";
import GiveButton from "@shared/Button/GiveButton";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import GiveText from "@shared/Text/GiveText";
import { useEffect, useState } from "react";
import { useMutation } from "react-query";
import {
sendTfaVerificationCode,
verifyTfaVerificationCode,
} from "@services/api/2FA/tfaVerifications";
import OTPInput from "react-otp-input";
import { styled } from "@theme/v2/Provider";
import GiveAlert from "@shared/GiveAlert/GiveAlert";
import GiveLink from "@shared/Link/GiveLink";
import { useFingerprint } from "@hooks/common/useFingerprint";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
type Props = {
email: string;
onSuccessFn: () => void;
closeOnVerify?: boolean;
sendCodeOnMount?: boolean;
};
const VerifyIdentityModal = ({
email,
onSuccessFn,
closeOnVerify,
sendCodeOnMount = true,
}: Props) => {
const { open, onRemove } = useNiceModal();
const tfaVerificationsMutation = useMutation(sendTfaVerificationCode);
const tfaVerificationsTokenMutation = useMutation(verifyTfaVerificationCode);
const { fingerprint, fingerprintData } = useFingerprint();
const { isMobileView } = useCustomThemeV2();
const [otp, setOtp] = useState("");
const [timer, setTimer] = useState<number>(120);
const resendCode = () => {
tfaVerificationsMutation.mutate("email");
};
const handleChange = (otp: string) => setOtp(otp);
const handleResendClick = () => {
setTimer(120);
resendCode();
};
const verify = (e: any) => {
e.preventDefault();
tfaVerificationsTokenMutation.mutate(
{
token: otp,
fingerprintToken: fingerprint,
timezone: fingerprintData?.locales.timezone,
},
{
onSuccess: async () => {
onSuccessFn();
Iif (closeOnVerify) onRemove();
},
},
);
};
useEffect(() => {
Eif (!sendCodeOnMount) return;
resendCode();
}, []);
useEffect(() => {
setOtp("");
}, [open]);
useEffect(() => {
let interval: NodeJS.Timeout;
Eif (timer > 0) {
interval = setInterval(() => {
setTimer((prev) => prev - 1);
}, 1000);
}
return () => clearInterval(interval);
}, [timer]);
return (
<GiveBaseModal
open={open}
title="Verify Your Identity"
width="560px"
onClose={onRemove}
{...(isMobileView && {
height: "55%",
})}
buttons={
<>
<GiveButton
variant="ghost"
size="large"
label="Cancel"
onClick={onRemove}
/>
<GiveButton
size="large"
variant="filled"
label="Continue"
type="submit"
form="login-otp"
disabled={otp.length < 6 || tfaVerificationsTokenMutation.isLoading}
sx={{ border: "none" }}
/>
</>
}
>
<Container
component="form"
id="login-otp"
onSubmit={verify}
error={tfaVerificationsTokenMutation.isError}
>
<Stack gap={1.5}>
<GiveText variant="bodyS" color="secondary" textAlign="center">
We sent your code to{" "}
<GiveText component="span" variant="bodyS" color="primary">
{maskEmail(email)}
</GiveText>
, go ahead and enter it below.
</GiveText>
{tfaVerificationsTokenMutation.isError && (
<GiveAlert
Icon={<></>}
label="The code you entered is invalid"
type="error"
/>
)}
</Stack>
<Box
sx={{
"& div": {
gap: "16px",
},
}}
>
<OTPInput
value={otp}
onChange={handleChange}
numInputs={6}
renderInput={(props, index) => (
<>
<input {...props} data-testid={`otp-input-${index}`} />
{index === 2 && <GiveText variant="bodyM">-</GiveText>}
</>
)}
inputStyle={{
width: isMobileView ? "49px" : "54px",
height: isMobileView ? "54px" : "64px",
padding: "8px",
userSelect: "none",
fontSize: "18px",
fontWeight: 400,
}}
inputType="number"
containerStyle={{
margin: "0 auto",
gap: "8px",
}}
/>
</Box>
{timer === 0 ? (
<Stack spacing={0.5} alignItems="center">
<GiveText variant="bodyS" color="secondary">
Haven’t received the code?
</GiveText>
<GiveLink
color="secondary"
onClick={handleResendClick}
sx={{ alignItems: "center", textWrap: "nowrap" }}
link="#"
>
Resend it now
</GiveLink>
</Stack>
) : (
<GiveText variant="bodyS" color="secondary" textAlign="center">
Code sent
<br /> Resend available in{" "}
{`${Math.floor(timer / 60)}:${(timer % 60)
.toString()
.padStart(2, "0")}`}
</GiveText>
)}
</Container>
</GiveBaseModal>
);
};
const Container = styled(Box)<BoxProps & { error: boolean }>(
({ theme, error }) => ({
display: "flex",
flexDirection: "column",
alignItems: "center",
width: "100%",
gap: "40px",
"& input": {
borderRadius: "12px",
border: `solid 1.5px ${theme.palette?.border?.secondary}`,
"&:focus": {
border: "solid 1.5px transparent",
outline: "none",
backgroundImage: `linear-gradient(${theme.palette.surface?.primary}, ${theme.palette.surface?.primary}), ${theme.palette?.gradient["aqua-horizon"]?.border}`,
backgroundOrigin: "border-box",
backgroundClip: "padding-box, border-box",
},
...(error && {
border: `solid 1.5px ${theme.palette?.primitive?.error?.[50]}`,
}),
},
}),
);
export default VerifyIdentityModal;
|