Skip to content
Snippets Groups Projects
SignInForm.tsx 3.62 KiB
Newer Older
Defendi Alberto's avatar
Defendi Alberto committed
import React, { FC, useContext } from 'react';
Defendi Alberto's avatar
Defendi Alberto committed
import axios from 'axios';
Defendi Alberto's avatar
Defendi Alberto committed
import { useCookie } from 'hooks/useCookie';
import { AuthRoutes, NonAuthRoutes } from 'api/routes';
import { SubmitHandler, useForm } from 'react-hook-form';
fmazzini's avatar
fmazzini committed
import {
  Button,
  createMuiTheme,
  responsiveFontSizes,
  MuiThemeProvider,
  Grid,
  Typography,
  Link,
} from '@material-ui/core';
Defendi Alberto's avatar
Defendi Alberto committed
import { useHistory } from 'react-router-dom';
import { InputField } from 'components/Auth/InputField/InputField';
import { signInFormStyles } from './SignInForm.styles';
import { CredentialsType } from './CredentialsType';
  username: '',
  password: '',
export const SignInForm: FC = () => {
Defendi Alberto's avatar
Defendi Alberto committed
  const history = useHistory();
Defendi Alberto's avatar
Defendi Alberto committed
  const cookie = useCookie();
  const { control, errors, setError, handleSubmit } = useForm<CredentialsType>({
    defaultValues,
  });

  const onSubmit: SubmitHandler<CredentialsType> = (
    values: CredentialsType,
  ) => {
Defendi Alberto's avatar
Defendi Alberto committed
    axios
      .post(
        '/api/web/login',
        {
          username: values.username,
Defendi Alberto's avatar
Defendi Alberto committed
          password: values.password,
          csrfmiddlewaretoken: cookie,
Defendi Alberto's avatar
Defendi Alberto committed
        {
          headers: {
            'Content-Type': 'application/json',
          },
        },
      )
      .then((response) => {
        if (response.data.status === 'fail') {
          setError('username', {
            type: 'server',
            message: 'Something went wrong with username',
          });
          setError('password', {
            type: 'server',
            message: 'Something went wrong with password',
          });
        } else if (response.data.status === 'role-choice-needed') {
          history.replace(`${NonAuthRoutes.auth}${AuthRoutes.choseRole}`);
        } else if (response.data.status === 'success') {
          history.replace(`${AuthRoutes.dashboard}${AuthRoutes.home}`);
Defendi Alberto's avatar
Defendi Alberto committed
      });
  const classes = signInFormStyles();
Defendi Alberto's avatar
Defendi Alberto committed
  return (
Francesco's avatar
Francesco committed
      <header>
Defendi Alberto's avatar
Defendi Alberto committed
        <Grid item container>
          <Grid item xs={12} color="primary" className={classes.paddingTop}>
            <Typography variant="h1">SIGN IN</Typography>
fmazzini's avatar
fmazzini committed
          </Grid>
Defendi Alberto's avatar
Defendi Alberto committed
        </Grid>
Francesco's avatar
Francesco committed
      </header>
fmazzini's avatar
fmazzini committed

      <form
        className={classes.form}
        onSubmit={handleSubmit(onSubmit)}
        data-testid="Form"
Defendi Alberto's avatar
Defendi Alberto committed
      >
          name="username"
          control={control}
Defendi Alberto's avatar
Defendi Alberto committed
          rules={{
            required: {
              value: true,
              message: 'Username is not valid',
Defendi Alberto's avatar
Defendi Alberto committed
            },
          }}
fmazzini's avatar
fmazzini committed
          label="Username"
          type="username"
          error={!!errors.username}
          errorMessage="Insert username"
Defendi Alberto's avatar
Defendi Alberto committed
        <InputField
Defendi Alberto's avatar
Defendi Alberto committed
          name="password"
          type="password"
Defendi Alberto's avatar
Defendi Alberto committed
          control={control}
          rules={{
            minLength: 8,
            maxLength: 60,
Defendi Alberto's avatar
Defendi Alberto committed
            required: {
              value: true,
Defendi Alberto's avatar
Defendi Alberto committed
              message: 'Insert valid password',
Defendi Alberto's avatar
Defendi Alberto committed
            },
Defendi Alberto's avatar
Defendi Alberto committed
          label="Password"
Defendi Alberto's avatar
Defendi Alberto committed
          error={!!errors.password}
Defendi Alberto's avatar
Defendi Alberto committed
          errorMessage="Insert valid password"
        <Button
Defendi Alberto's avatar
Defendi Alberto committed
          data-testid="Submit"
          type="submit"
          fullWidth
          variant="contained"
          color="primary"
          className={classes.submit}
        >
          Sign In
        </Button>
fmazzini's avatar
fmazzini committed

Defendi Alberto's avatar
Defendi Alberto committed
        <Grid item container>
          <Grid item xs={12} color="primary" className={classes.paddingBottom}>
            <Typography>
              New here? &nbsp;
              <Link href={`${NonAuthRoutes.auth}${NonAuthRoutes.signUp}`}>
                Create an account.
              </Link>
            </Typography>
fmazzini's avatar
fmazzini committed
          </Grid>
Defendi Alberto's avatar
Defendi Alberto committed
        </Grid>
      </form>
    </>
Defendi Alberto's avatar
Defendi Alberto committed
  );
};