All files / src/hooks/common useVerification.tsx

0% Statements 0/29
0% Branches 0/2
0% Functions 0/10
0% Lines 0/29

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                                                                                                                                                                                                     
import { showMessage } from "@common/Toast";
import { customInstance } from "@services/api";
import Cookies from "js-cookie";
import { useState } from "react";
import { SubmitHandler } from "react-hook-form";
import { useMutation } from "react-query";
import useGetCurrentMerchantId from "./useGetCurrentMerchantId";
import { verifyTfaVerificationCode } from "@services/api/2FA/tfaVerifications";
import { useFingerprint } from "./useFingerprint";
 
export const useCreateVerificationToken = ({
  action,
}: {
  action: React.Dispatch<React.SetStateAction<any>>;
}) => {
  const [isLoading, setIsLoading] = useState<boolean>(false);
 
  const tfaVerification = useMutation((data: Record<string, any>) => {
    return customInstance({
      url: `/tfa-verifications`,
      method: "POST",
      data,
    });
  });
 
  const handleMethodSubmit: SubmitHandler<Record<string, any>> = (data) => {
    setIsLoading(true);
 
    tfaVerification.mutate(data, {
      onSuccess: () => {
        showMessage(
          "Success",
          `Verification code sent! Check your ${
            data.method === "email" ? "email" : "SMS"
          }.`,
        );
        setIsLoading(false);
        action(2);
      },
      onError: (error: any) => {
        showMessage(
          "Error",
          "The provided password reset request is invalid and/or expired",
        );
        setIsLoading(false);
      },
    });
  };
  return {
    handleMethodSubmit,
    isLoading,
  };
};
 
export const useVerificationCode = ({
  action,
}: {
  action: React.Dispatch<React.SetStateAction<any>>;
}) => {
  const [isLoading, setIsLoading] = useState<boolean>(false);
  const { merchantId } = useGetCurrentMerchantId();
  const { fingerprint } = useFingerprint();
 
  const codeVerification = useMutation((code: string) => {
    return verifyTfaVerificationCode({
      token: code,
      fingerprintToken: fingerprint,
      trust: false,
    });
  });
 
  const handleCodeSubmit: SubmitHandler<Record<string, any>> = (data) => {
    setIsLoading(true);
    codeVerification.mutate(data.code, {
      onSuccess: () => {
        showMessage("Success", "Verification successful");
        Cookies.set(
          "transfer-verification",
          JSON.stringify({ date: new Date().toISOString(), id: merchantId }),
        );
        setIsLoading(false);
        action(3);
      },
      onError: (error: any) => {
        showMessage(
          "Error",
          "Code is not valid. It may be expired, you requested another code, or you made a typo. Double check and try again",
        );
        setIsLoading(false);
      },
    });
  };
 
  return {
    handleCodeSubmit,
    isLoading,
  };
};