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

97.22% Statements 35/36
92.85% Branches 13/14
100% Functions 4/4
97.14% Lines 34/35

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 1531x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x   7x                                                     1x 19x 18x 18x   18x   18x 18x   18x   18x         2x     18x 3x 1x 1x     2x     18x                   18x                                                                                                                                      
import { Container, Typography } from "@material-ui/core";
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 { useMutation } from "react-query";
import { loginRoute } from "routes";
import { service } from "service";
import makeStyles from "utils/makeStyles";
import stringOrFirstString from "utils/stringOrFirstString";
 
const useStyles = makeStyles((theme) => ({
  form: {
    "& > * + *": {
      marginBlockStart: theme.spacing(1),
    },
  },
  standardContainer: {
    marginTop: theme.spacing(2),
    paddingLeft: theme.spacing(2),
    paddingRight: theme.spacing(2),
    paddingBottom: theme.spacing(2),
    flex: 1,
  },
  main: {
    padding: theme.spacing(0, 3),
  },
  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 formClass = useStyles();
  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, isLoading, isSuccess, mutate } = useMutation<
    Empty,
    RpcError,
    string
  >((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 (
      <Container className={formClass.standardContainer}>
        <Alert severity="error">
          {t("auth:change_password_form.user_logged_error")}
        </Alert>
      </Container>
    );
  }
 
  return (
    <Container className={formClass.standardContainer}>
      <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>
 
      <form className={formClass.form} onSubmit={onSubmit}>
        <TextField
          className={formClass.textField}
          id="newPassword"
          inputRef={register({ required: true })}
          label={t("auth:change_password_form.new_password")}
          name="newPassword"
          type="password"
          variant="outlined"
        />
 
        <TextField
          className={formClass.textField}
          id="newPasswordCheck"
          inputRef={register({ required: true })}
          label={t("auth:change_password_form.confirm_password")}
          name="newPasswordCheck"
          type="password"
          variant="outlined"
        />
 
        <Button
          loading={isLoading}
          type="submit"
          disabled={isLoading || !isResetTokenOk || authState.authenticated}
        >
          {t("global:submit")}
        </Button>
      </form>
    </Container>
  );
}