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

91.66% Statements 33/36
80% Branches 8/10
83.33% Functions 5/6
91.17% Lines 31/34

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            2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x             2x   21x               21x                   9x 21x 21x 21x 21x 21x   21x           21x           3x 3x 3x 3x 3x                             3x                                                                             4x                                                                                            
import { Visibility, VisibilityOff } from "@mui/icons-material";
import {
  FormControlLabel,
  IconButton,
  InputAdornment,
  styled,
} from "@mui/material";
import CustomColorSwitch from "components/CustomColorSwitch";
import StyledLink from "components/StyledLink";
import { doAntibot } from "features/antibot/antibot";
import { useAuthContext } from "features/auth/AuthProvider";
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 { lowercaseAndTrimField } from "utils/validation";
 
import {
  StyledButton,
  StyledForm,
  StyledInputLabel,
  StyledTextField,
} from "../useAuthStyles";
 
const StyledFormControlLabel = styled(FormControlLabel)(({ theme }) => ({
  display: "block",
  marginInlineStart: 0,
  [theme.breakpoints.down("md")]: {
    marginBlockEnd: theme.spacing(1),
  },
}));
 
const StyledLoginOptions = styled("div")(({ theme }) => ({
  [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 { authState, authActions } = useAuthContext();
  const authLoading = authState.loading;
  const [loading, setLoading] = useState(false);
  const [showPassword, setShowPassword] = 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();
      doAntibot("login");
      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 (
    <>
      <StyledForm onSubmit={onSubmit}>
        <StyledInputLabel htmlFor="username">
          {t("auth:login_page.form.username_field_label")}
        </StyledInputLabel>
        <StyledTextField
          id="username"
          {...register("username", { required: true })}
          fullWidth
          variant="outlined"
          autoComplete="email"
          placeholder="you@couchers.org"
        />
        <StyledInputLabel htmlFor="password">
          {t("auth:login_page.form.password_field_label")}
        </StyledInputLabel>
        <StyledTextField
          id="password"
          {...register("password", { required: true })}
          fullWidth
          name="password"
          type={showPassword ? "text" : "password"}
          variant="outlined"
          autoComplete="current-password"
          slotProps={{
            input: {
              endAdornment: (
                <InputAdornment position="end" sx={{ marginRight: 1 }}>
                  <IconButton
                    aria-label={
                      showPassword
                        ? t("auth:login_page.form.hide_password")
                        : t("auth:login_page.form.show_password")
                    }
                    onClick={() => setShowPassword(!showPassword)}
                    edge="end"
                  >
                    {showPassword ? <VisibilityOff /> : <Visibility />}
                  </IconButton>
                </InputAdornment>
              ),
            },
          }}
        />
        <StyledLoginOptions>
          <Controller
            control={control}
            name="rememberDevice"
            defaultValue={true}
            render={({ field }) => (
              <StyledFormControlLabel
                control={
                  <CustomColorSwitch
                    size="small"
                    checked={field.value}
                    onClick={() => field.onChange(!field.value)}
                    isLoading={loading}
                  />
                }
                label={t("auth:login_page.form.remember_me")}
              />
            )}
          />
          <StyledLink href={resetPasswordRoute}>
            {t("auth:login_page.form.forgot_password")}
          </StyledLink>
        </StyledLoginOptions>
        <StyledButton
          loading={loading || authLoading}
          onClick={onSubmit}
          type="submit"
          variant="contained"
          fullWidth
        >
          {t("global:login")}
        </StyledButton>
      </StyledForm>
    </>
  );
}