All files / app/features/auth/password CompleteResetPassword.tsx

100% Statements 37/37
90.9% Branches 10/11
100% Functions 6/6
100% Lines 34/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 1521x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x   16x               16x           32x                   7x 16x 16x 16x         16x 16x   16x   16x           2x           16x 3x 1x 1x     2x     16x                                                                                                                                                    
import { Container, styled, Typography } from "@mui/material";
import { useMutation } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import HtmlMeta from "components/HtmlMeta";
import StyledLink from "components/StyledLink";
import TextField from "components/TextField";
import { useAuthContext } from "features/auth/AuthProvider";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { useRouter } from "next/router";
import { useForm } from "react-hook-form";
import { loginRoute } from "routes";
import { service } from "service";
import { theme } from "theme";
import stringOrFirstString from "utils/stringOrFirstString";
 
const StyledContainer = styled(Container)(() => ({
  marginTop: theme.spacing(2),
  paddingLeft: theme.spacing(2),
  paddingRight: theme.spacing(2),
  paddingBottom: theme.spacing(2),
  flex: 1,
}));
 
const StyledForm = styled("form")(() => ({
  "& > * + *": {
    marginBlockStart: theme.spacing(1),
  },
}));
 
const StyledTextField = styled(TextField)(() => ({
  "& > div": {
    width: "100%",
    marginBottom: theme.spacing(2),
    [theme.breakpoints.up("md")]: {
      width: theme.typography.pxToRem(400),
    },
  },
}));
 
export default function CompleteResetPassword() {
  const { authState } = useAuthContext();
  const { t } = useTranslation([AUTH, GLOBAL]);
  const { handleSubmit, register } = useForm<{
    newPassword: string;
    newPasswordCheck: string;
  }>();
 
  const router = useRouter();
  const resetToken = stringOrFirstString(router.query.token);
  const isResetTokenOk =
    !!resetToken && typeof resetToken === "string" && resetToken !== "";
 
  const { error, isPending, isSuccess, mutate } = useMutation<
    Empty,
    RpcError,
    string
  >({
    mutationFn: (newPassword) =>
      service.account.CompletePasswordResetV2(
        resetToken as string,
        newPassword,
      ),
  });
 
  const onSubmit = handleSubmit(({ newPassword, newPasswordCheck }) => {
    if (newPassword !== newPasswordCheck) {
      alert(t("auth:change_password_form.password_mismatch_error"));
      return;
    }
 
    mutate(newPassword);
  });
 
  Iif (authState.authenticated) {
    return (
      <StyledContainer>
        <Alert severity="error">
          {t("auth:change_password_form.user_logged_error")}
        </Alert>
      </StyledContainer>
    );
  }
 
  return (
    <StyledContainer>
      <HtmlMeta title={t("auth:change_password_form.title")} />
 
      {!isResetTokenOk && (
        <Alert severity="error">
          {t("auth:change_password_form.token_error")}
        </Alert>
      )}
 
      {error && (
        <Alert severity="error">
          {t("auth:change_password_form.reset_password_error", {
            message: error.message,
          })}
        </Alert>
      )}
 
      {isSuccess && (
        <>
          <Alert severity="success">
            {t("auth:change_password_form.reset_password_success")}
          </Alert>
          <StyledLink href={loginRoute}>{t("auth:login_prompt")}</StyledLink>
        </>
      )}
 
      <Typography variant="h1" gutterBottom>
        {t("auth:change_password_form.title")}
      </Typography>
 
      <Typography variant="body1" gutterBottom>
        {t("auth:change_password_form.subtitle")}
      </Typography>
 
      <StyledForm onSubmit={onSubmit}>
        <StyledTextField
          id="newPassword"
          {...register("newPassword", { required: true })}
          label={t("auth:change_password_form.new_password")}
          name="newPassword"
          type="password"
          variant="outlined"
        />
 
        <StyledTextField
          id="newPasswordCheck"
          {...register("newPasswordCheck", { required: true })}
          label={t("auth:change_password_form.confirm_password")}
          type="password"
          variant="outlined"
        />
 
        <Button
          loading={isPending}
          type="submit"
          disabled={isPending || !isResetTokenOk || authState.authenticated}
        >
          {t("global:submit")}
        </Button>
      </StyledForm>
    </StyledContainer>
  );
}