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 { AuthContext } from 'components/AuthUser/AuthContext';
import { fetchCookie } from './fetchCookie';
import { CredentialsType } from './CredentialsType';
const defaultValues: CredentialsType = {
username: '',
password: '',
const [isCookieFetched, setIsCookieFetched] = useState<string>('');
if (!isCookieFetched) fetchCookie(setIsCookieFetched);
const { setRole } = useContext(AuthContext);
const { control, errors, setError, handleSubmit } = useForm<CredentialsType>({
const onSubmit: SubmitHandler<CredentialsType> = (
values: CredentialsType,
) => {
csrfmiddlewaretoken: isCookieFetched,
{
headers: {
'Content-Type': 'application/json',
},
},
)
.then((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') {
history.replace(AuthRoutes.dashboard);
}
<>
<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>
</>