Newer
Older
import React, { FC, useContext, useEffect, useState } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { Button } from '@material-ui/core';
import { InputField } from 'components/AuthUser/InputField/InputField';
import { AuthRoutes, NonAuthRoutes } from 'api/routes';
import { AuthContext } from 'components/AuthUser/AuthContext';
import { CredentialsType } from './CredentialsType';
const defaultValues = {
username: '',
password: '',
const { setRole, setIsAuth } = useContext(AuthContext);
const { control, errors, setError, handleSubmit } = useForm<CredentialsType>({
const onSubmit: SubmitHandler<CredentialsType> = (
values: CredentialsType,
) => {
csrfmiddlewaretoken: localStorage.getItem('COOKIE'),
{
headers: {
'Content-Type': 'application/json',
},
},
)
console.log(response);
if (response.data.status === 'fail') {
message: 'Something went wrong with username',
});
setError('password', {
type: 'server',
message: 'Something went wrong with password',
});
} else if (response.data.status === 'success') {
setIsAuth(true);
history.replace(`${AuthRoutes.dashboard}${AuthRoutes.home}`);
<>
<form
className={classes.form}
onSubmit={handleSubmit(onSubmit)}
data-testid="Form"
label="username"
error={!!errors.username}
errorMessage="Insert username"
control={control}
rules={{
minLength: 8,
maxLength: 60,
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
>
Sign In
</Button>
</form>
</>