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

70.58% Statements 36/51
68% Branches 17/25
61.53% Functions 8/13
72.91% Lines 35/48

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                        36x 105x 105x 103x     36x                   36x 28x                     36x 28x 28x 28x 28x   28x   28x       28x                                       28x   28x       28x     3x   3x         3x     3x     28x       28x           28x 12x 7x 7x   5x         28x 7x             28x             3x 3x         3x                                                  
import { yupResolver } from "@hookform/resolvers/yup";
import { Role } from "@redux/slices/auth/types";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
 
import React, { useState } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { useLocation } from "react-router-dom";
import * as Yup from "yup";
import { IFormInputs } from "./types";
import useHandleLogin from "./useHandleLogin";
import { VALIDATION_MESSAGES } from "@validation/messages";
 
export const getUserRole = (role: string) => {
  Iif (role === "submerchant") return "merchant";
  if (role === "enterprise") return "provider";
  return (role || "merchant") as Role;
};
 
const defaultValues = {
  email: "",
  password: "",
  remember: true,
  termsConditions: true,
};
 
// Yup mutates its internal state during validation.
// When the schema is reused, the resolver can freeze the error object,
// which causes "object is not extensible" errors in production, using a factory prevents this by giving a fresh schema each time.
const getSchema = () =>
  Yup.object({
    email: Yup.string()
      .required(VALIDATION_MESSAGES.EMAIL_REQUIRED)
      .email(VALIDATION_MESSAGES.INVALID_EMAIL),
    password: Yup.string().required(VALIDATION_MESSAGES.PASSWORD_REQUIRED),
    remember: Yup.boolean(),
    termsConditions: Yup.bool()
      .required(VALIDATION_MESSAGES.TERMS_REQUIRED)
      .oneOf([true], VALIDATION_MESSAGES.TERMS_REQUIRED),
  });
 
export const useLogin = () => {
  const { isRecaptchaFF } = useGetFeatureFlagValues();
  const [isLoginLoading, setIsLoginLoading] = useState(false);
  const isMount = React.useRef(false);
  const location = useLocation();
 
  const [isPasswordVisible, setIsPasswordVisible] = React.useState(false);
 
  const methods = useForm<IFormInputs>({
    resolver: yupResolver(getSchema()),
    defaultValues,
  });
  const { watch, setValue } = methods;
  const {
    onHandleSubmit,
    showAlert,
    setShowAlert,
    loginRef,
    resendCode,
    error2FA,
    is2FALoading,
    handleVerify,
    tmpStep2,
    setTmpStep2,
    isDifferentAccount,
    isLoading,
    setIsDifferentAccount,
    loginConfigs,
    handleLogin,
    getRecaptchaToken,
    onGoBack,
    isEmailSent
  } = useHandleLogin({ email: watch("email") });
 
  const togglePasswordVisible = () => {
    setIsPasswordVisible(!isPasswordVisible);
  };
 
  const onSubmit: SubmitHandler<IFormInputs & { captchaToken?: string }> = (
    data,
  ) => {
    Iif (!data.email || !data.password || isLoading) return;
 
    const customData = {
      email: data.email,
      password: data.password,
      hasAcceptedTC: data.termsConditions,
      captchaToken: data.captchaToken,
      onFinally: () => setIsLoginLoading(false),
    };
 
    onHandleSubmit(customData);
  };
 
  const onContinueAnyway = () => {
    loginConfigs.current && handleLogin(loginConfigs.current);
  };
 
  const onSwitchAccount = () => {
    setIsDifferentAccount(false);
    methods.setValue("email", "");
    methods.setValue("password", "");
  };
 
  React.useEffect(() => {
    if (!isMount.current) {
      isMount.current = true;
      return;
    }
    Iif (showAlert) {
      setShowAlert("");
    }
  }, [watch("email"), watch("password")]);
 
  React.useEffect(() => {
    Iif (location.state) {
      setValue("email", (location.state as any)?.email);
      setShowAlert((location.state as any)?.alert);
      isMount.current = false;
    }
  }, []);
 
  return {
    isLoading: isLoading || isLoginLoading,
    showAlert,
    location,
    methods,
    isPasswordVisible,
    onSubmit: (data: IFormInputs) => {
      setIsLoginLoading(true);
      Iif (isRecaptchaFF && process.env.VITE_RECAPTCHA_SITE_KEY) {
        getRecaptchaToken((captchaToken?: string) =>
          onSubmit({ ...data, captchaToken }),
        );
      } else {
        onSubmit(data);
      }
    },
    setShowAlert,
    togglePasswordVisible,
    isDifferentAccount,
    onSwitchAccount,
    onContinueAnyway,
    handleVerify: (e: any) => {
      loginRef.current = {
        ...loginRef.current,
        termsConditions: watch("termsConditions"),
      };
 
      handleVerify(e);
    },
    resendCode,
    error2FA,
    is2FALoading,
    tmpStep2,
    setTmpStep2,
    onGoBack,
    isEmailSent
  };
};