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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | 3x 141x 141x 141x 141x 141x 141x 141x 1x 1x 141x 141x 5x 141x 126x 125x 120x 126x 141x 1x 1x 141x 1x 1x 1x 141x 141x 846x 141x 141x 3x 849x 141x | import { maskEmail } from "@hooks/auth-api/useLoginOTP";
import { Box, BoxProps, FormControlLabel, Stack } from "@mui/material";
import { ArrowLeftIcon, InfoIcon } from "@phosphor-icons/react";
import { sendTfaVerificationCode } from "@services/api/2FA/tfaVerifications";
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
import { SubmitHandler } from "react-hook-form";
import OTPInput from "react-otp-input";
import { useMutation } from "react-query";
import GiveButton from "@shared/Button/GiveButton";
import GiveCheckbox from "@shared/GiveCheckbox/GiveCheckbox";
import GiveLink from "@shared/Link/GiveLink";
import GiveText from "@shared/Text/GiveText";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { styled, useAppTheme } from "@theme/v2/Provider";
import LoginMainContainer from "./components/LoginMainContainer";
type Props = {
onSubmit?: SubmitHandler<any>;
email: string;
isMobileView: boolean;
withLoginMainContainer?: boolean;
setTmpStep2?:
| Dispatch<
SetStateAction<{
email: string;
}>
>
| ((data: { email: string }) => void);
error: string;
isLoading?: boolean;
stripLoginContainer?: boolean;
handleVerify: (data: {
token: string;
trust: boolean;
email: string;
onSuccessFn: (data: {
password: string;
remember: boolean;
email: string;
termsConditions: boolean;
}) => void;
}) => void;
forceUntrust?: boolean;
cancel?: () => void;
onGoBack?: () => void;
};
const GiveLoginOTP = ({
onSubmit,
email,
isMobileView,
withLoginMainContainer = true,
setTmpStep2,
handleVerify,
error,
isLoading = false,
stripLoginContainer = false,
forceUntrust = false,
cancel,
onGoBack,
}: Props) => {
const { palette } = useAppTheme();
const [otp, setOtp] = useState("");
const trustDeviceRef = useRef<boolean>(true);
const [timer, setTimer] = useState<number>(120);
// GB20910: tracks a resend-specific error so the user gets feedback if
// POST /tfa-verifications fails when they click "Resend it now".
const [resendError, setResendError] = useState<string>("");
const tfaVerificationsMutation = useMutation(sendTfaVerificationCode);
const resendCode = (data: "sms" | "email") => {
// Clear any previous resend error before each new attempt.
setResendError("");
tfaVerificationsMutation.mutate("email", {
// GB20910: Previously no onError was provided, so a failed resend was
// silent — the timer would reset but the user would never receive a code
// or any indication of what went wrong.
onError: () => setResendError("Failed to resend the code. Please try again."),
});
};
const handleChange = (otp: string) => setOtp(otp);
useEffect(() => {
setOtp("");
}, [email]);
useEffect(() => {
let interval: NodeJS.Timeout;
if (timer > 0) {
interval = setInterval(() => {
setTimer((prev) => prev - 1);
}, 1000);
}
return () => clearInterval(interval);
}, [timer]);
const handleResendClick = () => {
setTimer(120);
resendCode("email");
};
const verify = (e: any) => {
e.preventDefault();
const verifyObj = {
token: otp,
trust: forceUntrust ? forceUntrust : trustDeviceRef.current,
email: email,
onSuccessFn: onSubmit && (onSubmit as any),
};
handleVerify(verifyObj);
};
const handleGoBack = () => {
setTmpStep2?.({ email: "" });
onGoBack?.();
};
const content = (
<Container
component="form"
id="login-otp"
onSubmit={verify}
stripLoginContainer={stripLoginContainer}
error={Boolean(error)}
>
<Stack gap={1.5}>
<GiveText variant="h3" sx={{ fontWeight: 300 }}>
Verify your identity
</GiveText>
<GiveText variant="bodyS" color="secondary">
To continue, enter your 6 digit verification code sent to{" "}
<GiveText component="span" variant="bodyS" color="primary">
{maskEmail(email)}
</GiveText>
</GiveText>
</Stack>
<Stack gap="12px">
<Box sx={{ width: "100%", display: "flex", justifyContent: "center" }}>
<OTPInput
value={otp}
onChange={handleChange}
numInputs={6}
renderInput={(props, index) => (
<>
<input {...props} data-testid={`otp-input-${index}`} />
</>
)}
inputStyle={{
width: isMobileView ? "45px" : "54px",
height: "64px",
padding: "8px",
userSelect: "none",
fontSize: "18px",
fontWeight: 400,
color: error
? palette.primitive?.error?.[50]
: palette.text.primary,
}}
inputType="number"
containerStyle={{
margin: "0 auto",
gap: isMobileView ? "8px" : "16px",
}}
/>
</Box>
{!!error && (
<GiveText variant="bodyS" color="error" textAlign="center">
Invalid or expired code. Please try again.
</GiveText>
)}
</Stack>
<Stack gap="20px">
{timer === 0 ? (
<Stack direction="row" spacing={0.5} justifyContent="center">
<GiveText variant="bodyS" color="secondary">
Don’t see a 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">
Resend available in{" "}
{`${Math.floor(timer / 60)}:${(timer % 60)
.toString()
.padStart(2, "0")}`}
</GiveText>
)}
{/* GB20910: show error if the resend request to POST /tfa-verifications failed */}
{!!resendError && (
<GiveText variant="bodyS" color="error" textAlign="center">
{resendError}
</GiveText>
)}
{!forceUntrust && (
<Stack
direction="row"
alignItems="center"
justifyContent="center"
gap="4px"
>
<FormControlLabel
sx={{ m: 0, gap: "8px" }}
control={
<GiveCheckbox
defaultChecked
onChange={(e, checked) => (trustDeviceRef.current = checked)}
name="trust_device"
/>
}
label={<GiveText variant="bodyS">Trust this device</GiveText>}
/>
<GiveTooltip
heading="Trust this device"
title={
<Stack gap={2}>
<GiveText variant="bodyS" sx={{ color: palette.text.invert }}>
By checking this box, you are allowing this device to be
recognized as trusted. This means you won't need to
re-enter your credentials or complete additional
verification steps on this device in the future.
</GiveText>
<GiveText variant="bodyS" sx={{ color: palette.text.invert }}>
<b>Important</b>: If you use this device from a different
network or if it's shared with others, ensure it's
secure before trusting it. You can manage trusted devices in
your account > Security.
</GiveText>
</Stack>
}
fluidWidth
placement="bottom-start"
>
<InfoIcon size={16} color={palette.icon?.["icon-secondary"]} />
</GiveTooltip>
</Stack>
)}
</Stack>
<Box display="flex" justifyContent="space-between">
<GiveLink
onClick={() => (setTmpStep2 ? handleGoBack() : cancel?.())}
variant="bodyS"
Icon={setTmpStep2 ? ArrowLeftIcon : undefined}
>
{setTmpStep2 ? "Back" : "Cancel"}
</GiveLink>
<GiveButton
size="large"
variant="filled"
label="Continue"
type="submit"
form="login-otp"
disabled={otp.length < 6 || isLoading}
sx={{ border: "none" }}
/>
</Box>
</Container>
);
Iif (!withLoginMainContainer) return content;
return <LoginMainContainer>{content}</LoginMainContainer>;
};
export default GiveLoginOTP;
const Container = styled(Box, {
shouldForwardProp: (prop) =>
prop !== "error" && prop !== "stripLoginContainer",
})<BoxProps & { error: boolean; stripLoginContainer?: boolean }>(
({ theme, error }) => ({
display: "flex",
flexDirection: "column",
width: "100%",
gap: "32px",
"& 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]}`,
}),
},
}),
);
|