Skip to content
Snippets Groups Projects
SignInForm.tsx 4.09 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 { AuthContext } from 'components/Auth/AuthContext';
import { InputField } from 'components/Auth/InputField/InputField';
import { useStyles } from './useStyles';
import { CredentialsType } from './CredentialsType';
fmazzini's avatar
fmazzini committed
let themeResp = createMuiTheme();
themeResp = responsiveFontSizes(themeResp);

  username: '',
  password: '',
export const SignInForm: FC = () => {
Defendi Alberto's avatar
Defendi Alberto committed
  const history = useHistory();
  const { setRole, setIsAuth } = useContext(AuthContext);
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') {
Defendi Alberto's avatar
Defendi Alberto committed
          setRole(response.data.role);
          setIsAuth(true);
          history.replace(`${AuthRoutes.dashboard}${AuthRoutes.home}`);
Defendi Alberto's avatar
Defendi Alberto committed
      });
Defendi Alberto's avatar
Defendi Alberto committed
  const classes = useStyles();
  return (
Francesco's avatar
Francesco committed
      <header>
        <MuiThemeProvider theme={themeResp}>
          <Grid item container>
            <Grid item xs={12} color="primary" className={classes.paddingTop}>
              <Typography variant="h1">SIGN IN</Typography>
            </Grid>
fmazzini's avatar
fmazzini committed
          </Grid>
Francesco's avatar
Francesco committed
        </MuiThemeProvider>
      </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

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