All files / src/hooks/auth-api useLoginOTP.tsx

68.18% Statements 15/22
80% Branches 4/5
28.57% Functions 2/7
68.18% Lines 15/22

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                            202x   202x 50x     152x     152x     46x 28x                     28x 28x 28x 28x 28x   28x             28x                                                                           28x                  
import { useFingerprint } from "@hooks/common/useFingerprint";
import {
  sendTfaVerificationCode,
  verifyTfaVerificationCode,
} from "@services/api/2FA/tfaVerifications";
import { useRef, useState } from "react";
import { useMutation, UseMutationOptions } from "react-query";
 
type IFormInputs = {
  otp_input: string;
  trust_device: boolean;
};
 
export function maskEmail(email: string, asterisksLength?: number): string {
  const [username, domain] = email.split("@");
 
  if (username.length <= 2) {
    return `${username.charAt(0)}*${"@" + domain}`;
  }
 
  const maskedUsername = `${username.charAt(0)}${"*".repeat(
    asterisksLength || 12,
  )}${username.charAt(username.length - 1)}`;
  return `${maskedUsername}@${domain}`;
}
 
export const useLoginOTP = () => {
  const loginRef = useRef<{
    password: string;
    remember: boolean;
    email: string;
    termsConditions: boolean;
  }>({
    password: "",
    remember: false,
    email: "",
    termsConditions: false,
  });
  const [error, setError] = useState<string>("");
  const [isLoading, setIsLoading] = useState<boolean>();
  const tfaVerificationsMutation = useMutation(sendTfaVerificationCode);
  const { fingerprint, fingerprintData } = useFingerprint();
  const tfaVerificationsTokenMutation = useMutation(verifyTfaVerificationCode);
 
  const resendCode = (
    data: "sms" | "email",
    options: Omit<UseMutationOptions<any, any, any, any>, "mutationFn"> = {},
  ) => {
    tfaVerificationsMutation.mutate("email", options);
  };
 
  const handleVerify = (data: {
    token: string;
    trust: boolean;
    email: string;
    onSuccessFn: (data: {
      password: string;
      remember: boolean;
      email: string;
      termsConditions: boolean;
    }) => void;
  }) => {
    setIsLoading(true);
    tfaVerificationsTokenMutation.mutate(
      {
        token: data.token,
        trust: data.trust,
        fingerprintToken: fingerprint,
        timezone: fingerprintData?.locales.timezone,
      },
      {
        onError: (res: any) => {
          setError("The code you entered is invalid");
        },
        onSuccess: async (res: any) => {
          setError("");
          data.onSuccessFn({
            email: loginRef.current.email,
            password: loginRef.current.password,
            remember: loginRef.current.remember,
            termsConditions: loginRef.current.termsConditions,
          });
        },
        onSettled: () => {
          setIsLoading(false);
        },
      },
    );
  };
  return {
    error,
    isLoading,
    resendCode,
    loginRef,
    handleVerify,
    setError,
  };
};