All files / app/features/auth/login LoginForm.tsx

91.89% Statements 34/37
50% Branches 2/4
80% Functions 4/5
91.66% Lines 33/36

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 1421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   2x                                     1x 16x 16x 16x 16x 16x 16x     16x   16x           2x 2x 2x 2x                             2x       16x                                                               16x                                                                    
import { FormControlLabel, InputLabel } from "@material-ui/core";
import Button from "components/Button";
import CustomColorSwitch from "components/CustomColorSwitch";
import StyledLink from "components/StyledLink";
import TextField from "components/TextField";
import { useAuthContext } from "features/auth/AuthProvider";
import useAuthStyles from "features/auth/useAuthStyles";
import { useTranslation } from "i18n";
import { AUTH, GLOBAL } from "i18n/namespaces";
import Sentry from "platform/sentry";
import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { resetPasswordRoute } from "routes";
import isGrpcError from "service/utils/isGrpcError";
import makeStyles from "utils/makeStyles";
import { lowercaseAndTrimField } from "utils/validation";
 
const useStyles = makeStyles((theme) => ({
  rememberSwitch: {
    display: "block",
    marginInlineStart: 0,
    [theme.breakpoints.down("sm")]: {
      marginBlockEnd: theme.spacing(1),
    },
  },
  loginOptions: {
    [theme.breakpoints.up("md")]: {
      alignItems: "center",
      display: "flex",
      marginTop: theme.spacing(2),
      justifyContent: "space-between",
      width: "100%",
    },
  },
}));
 
export default function LoginForm() {
  const { t } = useTranslation([AUTH, GLOBAL]);
  const classes = useStyles();
  const authClasses = useAuthStyles();
  const { authState, authActions } = useAuthContext();
  const authLoading = authState.loading;
  const [loading, setLoading] = useState(false);
 
  const { handleSubmit, register, control } =
    useForm<{ username: string; password: string; rememberDevice: boolean }>();
 
  const onSubmit = handleSubmit(
    async (data: {
      username: string;
      password: string;
      rememberDevice: boolean;
    }) => {
      setLoading(true);
      authActions.clearError();
      try {
        authActions.passwordLogin({
          username: lowercaseAndTrimField(data.username),
          password: data.password,
          rememberDevice: data.rememberDevice,
        });
      } catch (e) {
        Sentry.captureException(e, {
          tags: {
            featureArea: "auth/login",
          },
        });
        authActions.authError(
          isGrpcError(e) ? e.message : t("global:error.fatal_message")
        );
      }
      setLoading(false);
    }
  );
 
  return (
    <>
      <form className={authClasses.form} onSubmit={onSubmit}>
        <InputLabel className={authClasses.formLabel} htmlFor="username">
          {t("auth:login_page.form.username_field_label")}
        </InputLabel>
        <TextField
          className={authClasses.formField}
          fullWidth
          id="username"
          inputRef={register({ required: true })}
          name="username"
          variant="standard"
        />
        <InputLabel className={authClasses.formLabel} htmlFor="password">
          {t("auth:login_page.form.password_field_label")}
        </InputLabel>
        <TextField
          className={authClasses.formField}
          fullWidth
          id="password"
          name="password"
          inputRef={register({ required: true })}
          type="password"
          variant="standard"
        />
        <div className={classes.loginOptions}>
          <Controller
            control={control}
            name="rememberDevice"
            defaultValue={true}
            render={({ onChange, value }) => (
              <FormControlLabel
                className={classes.rememberSwitch}
                control={
                  <CustomColorSwitch
                    size="small"
                    checked={value}
                    onClick={() => onChange(!value)}
                    isLoading={loading}
                  />
                }
                label={t("auth:login_page.form.remember_me")}
              />
            )}
          />
          <StyledLink href={resetPasswordRoute}>
            {t("auth:login_page.form.forgot_password")}
          </StyledLink>
        </div>
        <Button
          classes={{
            label: authClasses.buttonText,
          }}
          className={authClasses.button}
          loading={loading || authLoading}
          onClick={onSubmit}
          type="submit"
          variant="contained"
        >
          {t("global:continue")}
        </Button>
      </form>
    </>
  );
}